# 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 :
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()

In :
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 :
random.randrange(10,20,2)

Out:
12

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

In :
random.randrange(10,20,2)

Out:
18

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

In :
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 :
x = [1,50,67,900,10045]

In :
random.choice(x)

Out:
1

Lets try again.

In :
random.choice(x)

Out:
10045

Lets try a list with floating numbers.

In :
y = [1.1,50.1,67.5,900.5,10045.6]

In :
random.choice(y)

Out:
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 :
random.shuffle(y)

In :
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 :
random.random()

Out:
0.9429593602379831

Lets try again.

In :
random.random()

Out:
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 :
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 :
list_of_numbers = [i for i in range(20)]

In :
list_of_numbers

Out:
[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 :
random.sample(list_of_numbers,6)

Out:
[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 :
random.uniform(0,1)

Out:
0.520339269464314

Lets generate a random number from the triangular distribution.

In :
random.triangular(0,5,4)

Out:
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 :
random.gauss(5,1)

Out:
7.386834685680185

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

In :
random.gauss(5,1)

Out:
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 :
import numpy as np
from numpy.random import rand

In :
rand(10)

Out:
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 :
from numpy.random import seed

In :
seed(4)

In :
rand(10)

Out:
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 :
seed(4)
rand(10)

Out:
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 :
from numpy.random import randint

In :
randint(0,100)

Out:
44

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

In :
randint(0,100,2)

Out:
array([38, 52])

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

Lets first import numpy randn

In :
from numpy.random import randn

In :
seed(5)
randn(10)

Out:
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 :
seed(5)
randn(2,2)

Out:
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.