What is Random Number Generator in Python and how to use it?

Last updated on Jul 15,2021 18.6K Views

What is Random Number Generator in Python and how to use it?

edureka.co

While creating software, our programs generally require to produce various items. This is most common in applications such as gaming, OTP generation, gambling, etc. Python makes the task of generating these values effortless with its built-in functions. This article on Random Number Generators in Python, you will be learning how to generate numbers using the various built-in functions.

Before moving on, let’s take a look at the topics discussed in this tutorial:

So let’s begin. :)

What is Random Number Generator in Python?

Generators are functions that produce items whenever they are called. Random Number Generator in Python are built-in functions that help you generate numbers as and when required. These functions are embedded within the random module of Python.

Take a look at the following table that consists of some important random number generator functions along with their description present in the random module:

Function

Description

seed()

Values produced will be deterministic, meaning, when the seed number is the same, the same sequence of values will be generated

randrange()

Can return random values between the specified limit and interval

randint()

Returns a random integer between the given limit

choice()

Returns a random number from a sequence

shuffle()

Shuffles a given sequence

sample()

Returns randomly selected items from a sequence

uniform()

Returns floating-point values between the given range

Now let us take a deeper look at each of these.

Generating integers:

Random integers can be generated using functions such as randrange() and randint().

Let us first take a look at randint().

randint():

This function generates integers between a given limit. It takes two parameters where the first parameter specifies the lower limit and the second one specifies the upper limit. randint(a,b) starts generating values from a to b such that:

a <= x <= b   (includes a and b)

EXAMPLE:

import random
random.randint(2,9)

OUTPUT:   5

The above code can generate numbers from 2 to 9 including the limits. In case you want to generate several values between this range, you can make use of the for loop as follows:

EXAMPLE:

import random
for x in range(2):
    print(random.randint(2,9))

OUTPUT:

2
6

In case you want to generate numbers in intervals, you can use the randrange() function.

randrange():

The randrange() function, as mentioned earlier, allows the user to generate values by stepping over the interval count.

EXAMPLE:

import random
for x in range(5):
    print(random.randrange(2,60,2))

OUTPUT:

34
28
14
8
26

As you can see, all the numbers generated here are even numbers between 2 and 6.

You can also generate floating-point values using the built-in functions of the random module.

Generating floating-point values:

To generate floating-point numbers, you can make use of random() and uniform function.

random():

This function produces floating-point values between 0.0 to 1.0 and hence, takes no parameters. Please note that the upper limit is excluded. So the maximum value will be 9.999.

EXAMPLE:

import random
for x in range(5):
    print(random.random())

OUTPUT:

0.18156025373128404
0.19729969175918416
0.6998756928129068
0.16706232338156568
0.059292088577491575

uniform():

Unlike random() function, this function takes two parameters that determine the lower and the upper limits respectively.

EXAMPLE:

for x in range(5):
    print(random.uniform(6))

OUTPUT:

2.3135197730563335
5.752723932545697
4.561236813447408
3.8459675873377863
4.8252929712263235

Python also allows you to generate random values from a given sequence as well.

Generating values from a given sequence:

This can be done using choice() and sample() functions.

choice():

This function basically takes a sequence as a parameter and returns random values from it.

EXAMPLE:

for x in range(3):
    print(random.choice([1,2,3,4,5,6,7,8,9]))

OUTPUT:

3
1
4

As you can see, in the above output three values are returned using the for loop and all the values are taken randomly from the given list.

sample():

The sample()  function picks up a random sequence from the given sequence and returns it as the output. It takes two parameters where the first parameter is a sequence and second is integer value specifying how many values need to be returned in the output.

EXAMPLE:

print(random.sample([1,2,3,4,5,6,7,8,9],4))

OUTPUT:    [1, 4, 5, 9]

As you can see, the output list produced in the above example consists of four randomly selected values from the given sequence.

Other functions:

seed():

The seed() function takes a number as a parameter called the seed and produces same random numbers each time you call this function with that number.

EXAMPLE:

random.seed(2)
print(random.random(),random.random(),random.random(),end='nn')
random.seed(3)
print(random.random(),random.random(),random.random(),end='nn')
random.seed(2)
print(random.random(),random.random(),random.random())

OUTPUT:

0.9560342718892494 0.9478274870593494 0.05655136772680869

0.23796462709189137 0.5442292252959519 0.36995516654807925

0.9560342718892494 0.9478274870593494 0.05655136772680869

In the above example, the output for seed(2) is the same each time it is called. This function is very useful in experiments where you need to pass the same random numbers to various test cases.

shuffle():

This function is used to shuffle a given sequence randomly.

EXAMPLE:

mylist=[1,2,3,4,5,6,7,8,9]
random.shuffle(mylist)
print(mylist)

OUTPUT:    [6, 8, 2, 4, 3, 7, 1, 5, 9]

This brings us to the end of this article on “Random number generator in Python”. I hope you have understood all the concepts.

Make sure you practice as much as possible and revert your experience.  

Got a question for us? Please mention it in the comments section of this “Random Number Generator in Python” blog and we will get back to you as soon as possible.

To get in-depth knowledge on Python along with its various applications, you can enroll for the best Python course with 24/7 support and lifetime access. 

Upcoming Batches For Data Science with Python Certification Course
Course NameDateDetails
Data Science with Python Certification Course

Class Starts on 14th December,2024

14th December

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR Time Series Analysis using Python