Python Random Number With Examples

In this tutorial, we are going to learn about Python Random Number. In our previous tutorial, we learned about Python math module.

Python Random Number

To work with python random number, we need to import Python’s random module at first. Python random module provides pseudo-randomness.

Python random module uses Mersenne Twister as the core random generator. So, this module is completely unsuitable for cryptographic purposes for being deterministic. However, we can use Python’s random module for most of the cases because Python’s random module contains many well known random distributions.

Python Random Integer

In this section, we will be discussing about generation integer numbers randomly. We can use randint(a,b) function to get a random integer from range a to b. Again, we can get number from a sequence by using randrange(start, stop, step) function. Let’s see an example to get python random integer.

import random as rand
a = 10
b = 100
print('na=", a, "and b =', b)
print('printing number [', a, ', ', b, ') :', rand.randint(a,b))
start = 1
stop = 12
step = 2
print('nsequence = [1, 3, 5, 7, 9, 11]')
print('printing one number from the sequence :', rand.randrange(start, stop, step))

For each run, the output will change. However, here given a sample output.


Python Random Float

There are several functions that returns real number or float randomly. For example, random() function returns a real number from 0 to 1 (exclusive).

Again, uniform(a, b) functions return a real number from a to b. Moreover there are some random distributions also available in Python random module. We can also get real number from those distribution.

We can get random numbers from exponential distribution by using expovariate(lambd) function.

import random as rand
print('Random number from 0 to 1 :', rand.random())
print('Uniform Distribution [1,5] :', rand.uniform(1, 5))
print('Gaussian Distribution mu=0, sigma=1 :', rand.gauss(0, 1))
print('Exponential Distribution lambda = 1/10 :', rand.expovariate(1/10))

The values in output will vary for each execution. You will get output like this.

Random number from 0 to 1 : 0.5311529501408693
Uniform Distribution [1,5] : 3.8716411264052546
Gaussian Distribution mu=0, sigma=1 : 0.8779046620056893
Exponential Distribution lambda = 1/10 : 1.4637113187536595

Python Random seed

Python random number generation is based on the previous number, so using system time is a great way to ensure that every time our program runs, it generates different numbers.

We can use python random seed() function to set the initial value. Note that if our seed value doesn’t change in each execution, we will get same sequence of numbers. Below is a sample program to prove this theory about seed value.

import random
print('1st random number=", random.random())
print("2nd random number=", random.random())
print("1st random int=", random.randint(1, 100))
print("2nd random int=", random.randint(1, 100))
# resetting the seed to 10 i.e. first value
print("3rd random number=", random.random())
print("4th random number=", random.random())
print("3rd random int=", random.randint(1, 100))
print("4th random int=", random.randint(1, 100))

Below image shows the output produced by the python random seed example program. We will get the same sequence of random numbers for each run.


Python Random List – choice(), shuffle(), sample()

There are some functions to use randomness in a sequence. For example, using choice() function you can get a random element from a sequence.

Again, using shuffle() function you can shuffle the elements in a sequence.

Also, using sample() function you can get x number of elements from a sequence randomly. So, let’s see the following code for random list example.

import random as rand
# initialize sequences
string = "inconvenience"
l = [1, 2, 3, 4, 10, 15]
# get a single element randomly
print("Single character randomly chosen :', rand.choice(string))
print('one randomly chosen number :', rand.choice(l))
# get multiple element
print('Randomly chosen 4 character from string :', rand.sample(string, 4))
print('Randomly chosen 4 length list :', rand.sample(l, 4))
# shuffle the list
print('list is shuffled :', l)  # print the list

You may get output like the following.

Single character randomly chosen : i
one randomly chosen number : 10
Randomly chosen 4 character from string : ['e', 'c', 'n', 'n']
Randomly chosen 4 length list : [2, 10, 3, 15]
list is shuffled : [2, 4, 15, 3, 10, 1]

So, that’s all for python random number. To know more, see their official documentation.

By admin

Leave a Reply

%d bloggers like this: