# Working with Random Numbers in Python

• Lalit Salunkhe
• Jul 08, 2021
• Python Programming Randomness is the basis of statistics. Therefore, random numbers are an integral part of any statistical analysis. Going further, the new stream of Data Science is no different. Often we come up with a situation where we want to explain a concept in short But have no dataset ready in front to do so.

The random numbers are a blessing in disguise during such situations. They allow you to explain that particular concept without being imposed on the actual dataset. This article will teach us about generating random numbers in python and the package that helps us generate those.

(Recommended blog: First Step towards Python)

## Random Numbers

Well, what are random numbers you will ask; if you have numbers occurring in a sequence and following these two rules -

• Every number should be coming within a given set of numbers uniformly at equal intervals, and

• Are unpredictable.

Meaning, it is impossible to predict which number will occur at every draw (unpredictability is the randomness), then the numbers are called random numbers.

(Must read: Data types in Python)

## Python Library random: Importing

As I said above, random numbers are essential for statisticians and data scientists. They can be used for various purposes such as to explain concepts, cryptography, game development, etc. In python, we have a library (module) named “random” which allows us to generate random numbers of our own. Importing this module into your python environment is simple. All you need to do is follow the code below:

``````#Importing python module random to generate random numbers

import random as rnd``````

The command above will import the module random is imported into your python environment with the alias “rnd”. Meaning, from now onwards, you can use “rnd” every time you need to call this module. Also, there is no output to show for this one.

### Generating Random Numbers Within Default Range 0 to 1

There are various ways to generate random numbers and the most common of them all is generating random numbers between 0 and 1. We have a built-in function under the module we installed above with the name random() that allows us to get this task done. See the code below which is pretty straight forward though.

``````#Importing python module random to generate random numbers

import random as rnd

#generating random numbers between 0 and 1

rnd_num = rnd.random()

print("My first random number is: ", rnd_num)``````

The function random() is called to generate a random number between [0, 1) and the results are stored in a variable named rnd_num. See the output for this code as shown below: Random number generated between 0 and 1

Note: A note for users here is, the function random() can never generate a random number with value 1. You can call the help() function on this function to get a detailed explanation about it.

If you run the same code again, you will get a different value between 0 and 1 as a random number. See the output below: Random Number Changes every time you run the code

Also, see another example where we generate more than one random number with the help of the random() function between 0 and 1. Following is the code:

``````#Importing python module random to generate random numbers

import random as rnd

#generating 10 random numbers between 0 and 1

for i in range(10):

rnd_num = rnd.random()

print(rnd_num)``````

Here, the for loop runs 10 times, generates, and prints the random number between 0 and 1 at every iteration. See an output as shown below: Generating more than one random number in python within 0 and 1

(Read also: Python to represent output)

### Generating Random Numbers Between any Given Range

The random() function looks great, but it has its limitations. It doesn’t go beyond the range of 0 and 1 to generate the random numbers. I mean, you will not always seek the random numbers between 0 and 1, right? The range should be flexible, isn’t it? So, how can we generate random numbers between any given range?

We have the uniform() function in the python random module that helps to generate random numbers between any given range. The syntax of the uniform function is as shown below:

``````uniform(int x, int y)

Where int x -  specifies the upper limit.

Int y - specifies the lower limit.``````

Let us see how this function works through an example code as below:

``````#Importing python module random to generate random numbers

import random as rnd

#generating random numbers between any given range

for i in range(10):

rnd_num = rnd.uniform(3, 6)

print(rnd_num)``````

So far so good! Here, we are trying to generate 10 random numbers ranging between 3 to 6 using the uniform() function from the random module. Let us see the output as shown below: Generating Random Numbers between different ranges

The uniform() function here works on similar lines of the uniform data distribution from statistics.

### Generating Integer Random Numbers

Until now, whatever random numbers we have generated are floating-point random numbers. Meaning, they have an integer + decimal part in them. What if we needed random numbers which are integers only?

Can we generate those? Of course, we can. We have a dedicated function named randint() to generate random integer numbers in python between a specified range of integers. It has syntax as shown below:

``randint(start, end)``

Where,

start - specifies the starting integer value (similar to upper limit)

end - specifies the ending integer value (similar to lower limit)

Let us see an example of the randint() function and how it generates random integers between specifies limits.

``````#Importing python module random to generate random numbers

import random as rnd

#generating random integers between any given range

for i in range(4):

rnd_num = rnd.randint(2, 10)

print(rnd_num)``````

Here, we are using the randint() function to generate four integer random numbers between 2 and 10. One thing to note here is, this function also considers the starting and ending limit values while generating random numbers (so don’t get confused if 2 or/and 10 appear in the output).

Also note that, while generating an integer random number, the possibility is highest that the system will generate the same random number twice. It should not be confused as each draw of the random number is independent of another and duplications are totally fine. See the output as shown below: Generating Integer Random Numbers Between Ranges

(Recommended blog: Julia vs Python)

### Generating Random Numbers with Steps

What if you want random integers between a specified range but with a step or increment value, can we generate such random numbers? Absolutely, yes! We have the randrange() function which allows us to generate random integers between two specified ranges with a step or increment value. The syntax of the function is as shown below:

``randrange(start, end, step)``

Where,

start - specifies the starting integer default is set to zero.

end - specifies the ending integer up to which the random numbers can be generated.

step - specifies the step/increment value.

See the code below for a better realization of how the function works.

``````#Importing python module random to generate random numbers

import random as rnd

#generating random integers between any given range with steps

for i in range(4):

rnd_num = rnd.randrange(1, 16, 4)

print(rnd_num)``````

Here, we are requesting four random numbers between 1 to 16 with an increment value of 4. Note that, you can still see the duplicates in the output. However, the difference between any two pairs or numbers will be 4. See the output below: The output of generating integer random numbers with a step value

Here, the values are 5, 5, 13, and 9. If you check any pair, the difference between them is four. Ex. 5 and 9, the difference is 4. 9, and 13, the difference is four. Don’t compare 5 and 13 with each other though :D

(Referred blog: Mickey: the voice assistant)

## Summary

• To generate random numbers in python, we have the random module/library which needs to be imported.

• The random() function allows us to generate random numbers between 0 and 1 (generates floating-point random numbers). It is a default random generator function.

• The uniform() function generates random numbers between specified ranges rather than 0 and 1 (generates floating-point random numbers).

• The randint() function generates random integers between specified ranges.

• The randrange() function generates random integers between specified ranges with step (increment) value.

0%  