There’s an easy way to generate random numbers with Python:
random module provides (pseudo-) random number generators. So just
To generate a random number use
By default you get a random number between 0 and 1.0. If you want a random number in a specified range or from a sequence you can do it in a couple of ways:
Click on the links above to jump to the method or read through the whole article.
Generates a random integer between 1 and 50.
Generates a random number with floats.
Random ints with steps
random.randrange(0, 200, 5)
will return a random integer between 0 and 200. The int provided will be always in 5 steps, meaning you get 10, 45, 20, 5, etc…
Select random element from a sequence
t = (5, 23, 768, 4, 1028)
will return one of the elements in