Table of contents
1.
Introduction
2.
Random Module
3.
choice()
3.1.
choices()
3.2.
seed()
3.3.
random()
3.4.
randint()
3.5.
randrange()
3.6.
shuffle()
3.7.
sample()
3.8.
getrandbits()
4.
FAQ'S
5.
Key Takeaways
Last Updated: Mar 27, 2024

Python Random Module

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In the programming world, the python language has attracted a lot of mass due to its English-like syntax and portable nature.

As we all know, inbuilt functions help ease our code readability, and we will cover the built-in module of Python, i.e., Random Module, which is used to generate random numbers. 

What do you mean by a random number?

These are pseudo-random numbers as the generated sequence depends on the seed.

Random Module offers some methods to generate random numbers.

Let's have a look at them through examples.

Also Read, Floor Division in Python and Convert String to List Python.

Also See, Python Round Function.

Random Module

choice()

This function will return a random element from the given sequence. The sequence can be a list, string, tuple, etc. The syntax will be as follows:

random.choice(sequence)

A sequence can be a tuple, a list, string, etc.

Example

Input

import random

s = "Hello"
print(random.choice(s))

list1 = ["Coding", "Ninjas"]
print(random.choice(list1))
You can also try this code with Online Python Compiler
Run Code

Output

1
Ninjas
You can also try this code with Online Python Compiler
Run Code

choices()

This function will return a list with the randomly selected item from the selected sequence. The sequence can be a tuple, a list, a string, a range, etc.

The syntax will be as follows:

random.choices(sequence, weights, cum_weight, k)

Sequence: The sequence can be a list, a string, a range, etc.

Weights: A list where you can weigh the possibility for each value.

Cum_weight: A list where you can weigh the possibility for each value, only this time the possibility is accumulated.

K: It will define the length of the returned list.

Example

Input

import random

list1 = ["Coding", "Ninjas", "CN"]

print(random.choices(list1, weights = [10, 1, 1], k = 6))
You can also try this code with Online Python Compiler
Run Code

Output

['Ninjas', 'Coding', 'Coding', 'Coding', 'Coding', 'Coding']
You can also try this code with Online Python Compiler
Run Code

seed()

This function is used to initialize the random numbers using seeding values. The seed() method will customize the start number of the random number generator. 

The syntax will be as follows:

random.seed(a, version)

a: The seed value to represent a random number. If the value is an integer, use it directly; otherwise, convert it into an integer.

version: It is an integer representing how to convert the 'a' parameter into an integer.

Example

Input

import random

random.seed(6)
print(random.random())

# Always generate the same random number as the first time.
print(random.random())
You can also try this code with Online Python Compiler
Run Code

Output

0.793340083761663
0.8219540423197268
You can also try this code with Online Python Compiler
Run Code

Note: The output of the above code will always be the same. Therefore, it must not be used for encryption.

random()

This function will return the random floating-point value between 0 to 1. The syntax will be as follows:

random.random()

No parameters.

Example

Input

import random

print(random.random())
You can also try this code with Online Python Compiler
Run Code

Output

0.8924279116755345
You can also try this code with Online Python Compiler
Run Code

randint()

This function will return the random integers between the specified range. The syntax will be as follows:

randint(start, end)

start: Specifying the position to start.

end: Specifying the position to end. 

Example

Input

import random

# Returns a random number between 5 and 10
print(random.randint(5, 10))

print(random.randint(-10,-5))
You can also try this code with Online Python Compiler
Run Code

Output

9
-10
You can also try this code with Online Python Compiler
Run Code

randrange()

This function will return the integer between the specified range and include steps. The syntax of this function will be as follows.

random.randrange(start, stop, step)

start: Specifying the position to start.

stop: Specifying the position to end.

step: Specifying the incrementation.

Example

Input

import random

# Here 5 is included but 10 is not included
print(random.randrange(5, 10))

# specifying the step as 2 also.
print (random.randrange(20,40,2))
You can also try this code with Online Python Compiler
Run Code

Output

8
34
You can also try this code with Online Python Compiler
Run Code

shuffle()

This function will return the reorganized form of the given sequence. Shuffling means changing the position of the items. This is to be done here using the shuffle() method. This method will change the original list.

The syntax will be as follows:

random.shuffle(sequence, action)

sequence: A sequence can be a tuple, a list, etc.

function: The function's name returns the number between 0.0 to 1.0.

Example 1

Input

import random

list1 = ["3", "1", "5"]
random.shuffle(list1)

# Printing after the shuffle
print(list1)
You can also try this code with Online Python Compiler
Run Code

Output

['1', '5', '3']
You can also try this code with Online Python Compiler
Run Code

Example 2

Input

import random

def func():
  return 0.3

# Specifying the function
list1 = ["2", "4", "6"]
random.shuffle(list1, func)

print(list1)
You can also try this code with Online Python Compiler
Run Code

Output

['4', '6', '2']
You can also try this code with Online Python Compiler
Run Code

sample()

This method will return a list with a selection of a specified number of items from the sequence. This method will not change the original sequence.

The syntax will be as follows:

random.sample(sequence, k)

sequence: A sequence can be a tuple, a list, a range, etc.

k: The size of the returned sequence.

Example

Input:

import random

list1 = ["1", "2", "hello", "5"]

print(random.sample(list1, k=3))
You can also try this code with Online Python Compiler
Run Code

Output

['2', '5', '1']
You can also try this code with Online Python Compiler
Run Code

getrandbits()

This method will return an integer with a specified number of bits. The number of bits required will be passed as the parameter. The syntax will be as follows:

random.getrandbits(n)

n: Specifying the size of bits of the returned integer.

Example

Input

import random

print(random.getrandbits(10))
print(random.getrandbits(8))
You can also try this code with Online Python Compiler
Run Code

Output

157
198
You can also try this code with Online Python Compiler
Run Code

 

You can practice by yourself with the help of online python compiler.

Also see, How to Check Python Version in CMD

FAQ'S

1). What do you mean by random number generator?

A random number generator is an algorithm used to generate a random sequence of numbers.

2). What are the applications of generating random numbers?

It has a large application in advanced technology like Machine Learning, computer simulation, cryptography, etc.

Key Takeaways

The random module is used to generate random numbers in python, and it has many applications like statistics sampling, computer simulation, etc. We have covered a set of methods with examples of the random module in this article.

For more details about generating a random number, you can look at this.

Also, you can check out Modules & Packages in Python.

For more clarity and details regarding the Python language, one can refer to this article. 

Anyone interested in learning the python language can enroll in the intuitively designed courses on Coding Ninjas.

Coding Ninjas Studio is a one-stop destination for various DSA questions typically asked in interviews to practice more such problems. 

Happy Coding!

Live masterclass