8

I understand that I should use os.urandom() or SystemRandom in Python for 'secure' pseudo-random numbers.

But how does Python generate these random numbers logically?

Also is there a way to generate a number in Python that is 'more random' than others?

1
  • I've answered a very similar question as this in great detail(It covers both Random and SystemRandom). ANSWER LINK Commented Jul 25, 2019 at 3:05

1 Answer 1

7

For "secure" random numbers, Python doesn't actually generate them: it gets them from the operating system, which has a special driver that gathers entropy from various real-world sources, such as variations in timing between keystrokes and disk seeks.

Sign up to request clarification or add additional context in comments.

16 Comments

How can we be sure that these are truly random events? And would you happen to know what the driver is called?
It depends on the operating system. For Linux, read about /dev/random and /dev/urandom.
Thanks! I'll come back to upvote once I get the rep - lol. What about Windows?
Can you predict exactly the time to the next key press, or which key that will be, or when the next disk seek will happen, or how long it will take - all of these are essentially unpredictable (even with physical access to the machine) so they can be used to generate a random number - so long as they are combined in a way that maintains their key characteristics - that is what things like /dev/random does.
@BillalBEGUERADJ, the documentation for os.urandom explains exactly how it gets random data from the OS, and the documentation for SystemRandom says that it calls os.urandom. As for how the OS gets the data, you can read about how /dev/random and /dev/urandom work.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.