Would secrets.choice
be random enough to pick winners in a raffle?
1 Answer
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
.
-
$\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 callsecrets.choice(names)
it will be as secure as drawing a random paper ticket? $\endgroup$– SomeoneCommented May 3, 2022 at 16:08 -
1$\begingroup$
random
actually supports multiple generators includingSystemRandom
which uses the same urandom-or-equivalent source assecrets
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$– benrgCommented May 3, 2022 at 21:28 -
$\begingroup$ @benrg Thanks, I didn't know that, I will update my answer accordingly $\endgroup$– tur11ngCommented May 3, 2022 at 22:49