How to Generate Random Numbers in Python

There are different ways, we can generate random numbers in Python. We can categorize them in following ways.

Random numbers generate from a list using Python random

Random numbers using Numpy Random

Lets go through the above methods one by one.

We need random package from Python. Lets import that.

In [1]:
import random

Using Python random package we can generate random integer number, generate random number from sequence, generate random number from sample etc. Do random? to learn more about all these methods.

How To Generate Python Random Integer Number in Range Using random.randint()

Lets start with an example.

In [28]:
print(random.randint(-10,-1))
print(random.randint(0,100))
print(random.randint(900,1000))
-9
92
950

How To Generate Python Random Number in Range Using random.randrange()

The difference between random.randint() and random.randrange() method is that in random.randrange() we can give it a step size as shown below.

In [5]:
random.randrange(10,20,2)
Out[5]:
12

In randrange, first two numbers define the range and the last one is the step size. Lets try one more time.

In [6]:
random.randrange(10,20,2)
Out[6]:
18

Lets see if we can generate float number using random.randrange.

In [8]:
random.randrange(10.1,20.5,2)

I got following error...

ValueError: non-integer arg 1 for randrange()

random.randrange is only used for random integer number generation.

How To Generate Python Random Number From A Given User List Using random.choice()

We can also give random package a list of numbers and ask it to generate a random number.

Lets create a Python list of numbers

In [10]:
x = [1,50,67,900,10045]
In [11]:
random.choice(x)
Out[11]:
1

Lets try again.

In [12]:
random.choice(x)
Out[12]:
10045

Lets try a list with floating numbers.

In [35]:
y = [1.1,50.1,67.5,900.5,10045.6]
In [36]:
random.choice(y)
Out[36]:
67.5

Unlike random.randrange(), random.choice() works for floating numbers too.

How To Use random.shuffle() To Shuffle The List of Numbers

Well we can shuffle the list from using choice method. shuffle() will shuffle the list. Lets try it.

In [37]:
random.shuffle(y)
In [38]:
print(y)
[67.5, 900.5, 10045.6, 1.1, 50.1]

As we got the list shuffled with random.shuffle() method

How To Generate Python Floating Random Number Between 0 and 1 using random.random()

random.random() is based on famous pseduo random number generator Mersenne Twister.

In [39]:
random.random()
Out[39]:
0.9429593602379831

Lets try again.

In [40]:
random.random()
Out[40]:
0.025639681733548914

Every time we run random.random(), We get a new random floating point number between 0 and 1. By default these numbers are generated from the uniform distribution which means probability of drawing or generating any random number between 0 and 1 is equal.

To make sure that we generate the same number, every time we run random.random(), we can give it a seed. Lets first show you an example.

In [41]:
random.seed(4)
print(random.random())
random.seed(4)
print(random.random())
0.23604808973743452
0.23604808973743452

As see we above, we got the same random floating number, when we gave it the same seed. Rember the number that we passed to seed is artibrary number here, as long as it is same, you would get same random floating number.

How To Sample From Python List using random.sample()

Sampling is often used in machine learning techniques. Python random.sample() can be used to sub sample data.

Lets create a list of 20 numbers first.

In [43]:
list_of_numbers = [i for i in range(20)]
In [44]:
list_of_numbers
Out[44]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
In [ ]:
Now using random.sample() we can get random sub sample of numbers from our list. lets try that.
In [46]:
random.sample(list_of_numbers,6)
Out[46]:
[3, 12, 15, 4, 2, 1]
In [ ]:
second argument in random.sample() specifies how many random numbers we want from our original list.

How To Generate Random Numbers In Python From Various Distributions

Lets generate a random number from the uniform distribution.

In [54]:
random.uniform(0,1)
Out[54]:
0.520339269464314

Lets generate a random number from the triangular distribution.

In [57]:
random.triangular(0,5,4)
Out[57]:
4.069601373131586

In above command, the random number is generated between 0 and 5 and third argument is the mode. Please checkout wikipedia to learn more about the triangular distributions.

Lets generate a random number from most common distribution which is Gaussian distribution using random.gauss()

In [60]:
random.gauss(5,1)
Out[60]:
7.386834685680185

In random.gauss() first argument is the mean and 2nd argument sigma is standard deviation. Lets try one more time.

In [61]:
random.gauss(5,1)
Out[61]:
6.412253897680218

How To Generate Random Numbers In Python Using Numpy

Numpy has different pseudorandom generator implementation then Python. Numpy is very extensively used in machine learning and for doing complex matrix and mathematical calculations.

Using Numpy we can generate a sequence of random numbers with just one command.

Lets try numpy random.rand() function to generate 10 random numbers.

In [66]:
import numpy as np
from numpy.random import rand
In [68]:
rand(10)
Out[68]:
array([0.79538502, 0.62136366, 0.51994258, 0.908149  , 0.41365184,
       0.49549702, 0.17301808, 0.44668745, 0.3166521 , 0.10106421])

As we see above using numpy we generated random array of numbers.

Numpy also has seed function, which will make sure that we get same sequence of numbers when we run numpy rand using seed function.

In [69]:
from numpy.random import seed
In [70]:
seed(4)
In [71]:
rand(10)
Out[71]:
array([0.96702984, 0.54723225, 0.97268436, 0.71481599, 0.69772882,
       0.2160895 , 0.97627445, 0.00623026, 0.25298236, 0.43479153])
In [ ]:
Lets try generating the numpy random numbers using same seed 4.
In [72]:
seed(4)
rand(10)
Out[72]:
array([0.96702984, 0.54723225, 0.97268436, 0.71481599, 0.69772882,
       0.2160895 , 0.97627445, 0.00623026, 0.25298236, 0.43479153])

As we see above, we got the same sequence of numbers.

Similarly we can generate integers using numpy randint. Lets import randint.

In [73]:
from numpy.random import randint
In [75]:
randint(0,100)
Out[75]:
44

To generate more than one number, pass number argument to numpy randint() and we would get an array of numbers.

In [76]:
randint(0,100,2)
Out[76]:
array([38, 52])

How To Generate Numpy Array Of Random Numbers From Gaussian Distribution Using randn()

Lets first import numpy randn

In [77]:
from numpy.random import randn
In [81]:
seed(5)
randn(10)
Out[81]:
array([ 0.44122749, -0.33087015,  2.43077119, -0.25209213,  0.10960984,
        1.58248112, -0.9092324 , -0.59163666,  0.18760323, -0.32986996])

As we see above, numpy randn(10) generated 10 numbers for a mean of 0 and variance of 1.

We can also generate a matrix of numbers from Gaussian Distribution. Lets say we want to generate a 2x2 matrix of Gaussian numbers using numpy randn.

In [86]:
seed(5)
randn(2,2)
Out[86]:
array([[ 0.44122749, -0.33087015],
       [ 2.43077119, -0.25209213]])

Wrap Up!

I have shown above the usage of Python random package and Numpy random package to generate random numbers through examples. I hope you would find the post useful.