1
$\begingroup$

Would secrets.choice be random enough to pick winners in a raffle?

$\endgroup$

1 Answer 1

1
$\begingroup$

Yes it is. According to the Python documentation

The secrets module is used for generating cryptographically strong random numbers suitable for managing data such as passwords, account authentication, security tokens, and related secrets.

In the background, secrets doesn't use a PRNG like random. In Linux specifically it uses \dev\urandom which is considered cryptographically secure and is used by popular cryptographic libraries like OpenSSL. In Windows, it uses RtlGenRandom.

$\endgroup$
3
  • $\begingroup$ Thank you! So if I have a list of names ['John Smith', 'Jane Doe', 'Jane Doe', 'Joe Public'] (Jane Doe bought two tickets), and call secrets.choice(names) it will be as secure as drawing a random paper ticket? $\endgroup$
    – Someone
    Commented May 3, 2022 at 16:08
  • 1
    $\begingroup$ random actually supports multiple generators including SystemRandom which uses the same urandom-or-equivalent source as secrets does. secrets was added later as a module that would be harder to accidentally misuse if you need cryptographic randomness. @Someone, it's secure if you trust the computer you run it on. A paper drawing may (or may not) be more difficult to hack. $\endgroup$
    – benrg
    Commented May 3, 2022 at 21:28
  • $\begingroup$ @benrg Thanks, I didn't know that, I will update my answer accordingly $\endgroup$
    – tur11ng
    Commented May 3, 2022 at 22:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.