Python Random Module | Python random number

Python random number – Python has defined a set of functions or methods that are used to generate or modify random numbers through the random module. Functions in the random module trust a pseudo-random number generator function random(), which creates or generates a random float number between 0.0 and 1.0. These specific type of functions is used in a lot of games, lotteries, or any application that requires a random number generation.

SYNTAX - 

import random

num=random.random()

print(num)

For example – 

import random

print(random.randint(0,9))

OUTPUT –

5

Note that we can get different outputs because this program generates a random number in the range of 0 and 9. The syntax of this function is:

random.randint(a,b)

Following are the different ways to generate random numbers:

  1. choice()
  2. randrange(beg,end,step)
  3. seed()
  4. shuffle()
  5. uniform()

The pseudo-random numbers that the random module generates are not cryptographically secure or safe. Python uses the Mersenne Twister algorithm for creating or generating random numbers. But this algorithm is completely deterministic, making it a very unsuitable choice for cryptographic purposes.

Tags: No tags

Add a Comment

Your email address will not be published. Required fields are marked *