user7579349
user7579349

Reputation: 691

random.SystemRandom().choice() vs random.choice()

What is the difference between random.SystemRandom().choice() & random.choice() in python?

I have seen the former being used, in more than one place. But its not mention py2 or py3 documentation.

Upvotes: 11

Views: 3622

Answers (1)

user2357112
user2357112

Reputation: 280788

random.SystemRandom is a random number generator class intended for cryptographic use. It uses os.urandom for its underlying byte stream; os.urandom pulls from an OS-dependent cryptographic random number source, sometimes /dev/urandom (but not always, even when /dev/urandom exists.

The SystemRandom class provides all random number generation methods the random module itself does, with the same meanings, just using a cryptographic RNG to implement them. random.choice and the choice method of a SystemRandom instance both make a random choice from an input sequence, but only SystemRandom is suitable for cryptographic use. random.choice's choices can be predicted by an adversary without much difficulty.

Upvotes: 9

Related Questions