Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 807
user7579349 Avatar asked Dec 24 '19 21:12

user7579349


1 Answers

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.

like image 123
user2357112 supports Monica Avatar answered Oct 16 '22 19:10

user2357112 supports Monica