Randomness in Python: A Comprehensive Guide

Computers can’t generate a truly random number

Yang Zhou
TechToFreedom

--

Randomness in Python: 3 Types of Operations
Image by didssph from Pixabay

Randomness is one of the essential features of the world. In software development and data science, we usually need to handle random things.

However, computers are inherently not good at random stuff. As long as we use a human-defined algorithm to get a number, the machine will follow the same algorithm every time, so the results are predictable and lack randomness.

Therefore, computers can’t generate a truly random number, since they are designed to be deterministic. But using some tricks to generate pseudo-random numbers is totally possible. Typically, a pseudo-random generator starts with a “seed” number and then follows a pattern. So as long as the “seed” is different every time, the generated number will be different as well.

There are some choices of the pseudo-random generator. Python uses the Mersenne Twister as the core generator in its built-in random module. This article will dive into the random module and give you a comprehensive guide about the randomness in Python.

“One thing that traditional computer systems aren’t good at is coin flipping.” — Steve Ward, Professor of Computer Science and…

--

--