Computer >> Computer tutorials >  >> Programming >> Python

How does Python generate random numbers?


Standard distribution of Python has a random module that has functions for random number generation. Basic random() function returns a random floating point number between 0 and 1

>>> import random
>>> random.random()
0.5204702770265925

From same module, there is randrange() function which returns a random number between a sequential range.

>>> random.randrange(0,10)
4

There is also choice() function which randomly chooses an item from list or tuple

>>> random.choice([10,20,30,40,50])
40
>>> random.choice((10,20,30,40,50))
20