Loading, please wait...

MCQ

How to use random module ? | Python Tutorial

This article is about the operations or the functions that we can use with random module in python.

How to use Random Module? |Python Tutorial

In this article, we will learn how to use a random module to generate pseudo-random numbers. So let’s begin this journey. Before using this import the random module first. Use the following statement to import this module:

syntax:

import random

Operations 

That we can perform are listed below:

1. random()

2. randint()

3. randrange()

4. choice()

5. choices()

6. sample()

7. seed()

8. shuffle()

9. uniform()

10. triangular()

 

1. random():

   random() is the most basic function of the python random module. All the functions depend on this function.

syntax:

random.random()

It doesn’t take any argument and returns the next random floating-point number in the range [0.0, 1.0).

For Example:

#this code will returns any random number in the range of [0.0, 1.0)

import random

x= random.random()

print(x)

#output: 0.4289998617129115

2. randint():

This function can be used to generate a random integer within the specified range.

Syntax:

random.randint(a: int, b: int) 

For Example:

#this code generates a random integer value within the specified range

import random

x= random.randint(0,20)

print(x)

#output: 12

3. randrange():

    This function returns a randomly selected integer from the range(start, stop, step). 

Syntax:

random.randrange(start, stop, step)

For Example:

#This code will return any random number between 3 to 30, such as 3,6,9,12,15.....27

import random

x= random.randrange(3,30,2)

print(x)

#output: 3

4. choice(): 

    choice() is a built-in function in python that returns random items from a list, tuple, any sequence. 

Syntax:

random.choice(sequence)

 Parameters: 

 Sequence: It is a mandatory parameter and it can be a list, tuple, or any string. 

 For Example:

#This code will return a random element from the list.

import random

name_list = ['Bhumi' ,'Bhuvi' , 'Chotu' ,'Komal']

x= random.choice(name_list)

print(x)

#output: Bhumi

5. choices():

This function returns multiple random elements from list, tuple, or any sequence. 

 Syntax:

random.choices(sequence, weights =None, cum_weights=None, k=1)

Parameters:

sequence : (mandatory parameter) it can be a list, tuple, or string.

weights: It is an optional parameter that is used to weigh the possibility for each value.

cum_weight: It is an optional parameter that is used to weigh the possibility for each value but this possibility is accumulated.

k: it is an optional parameter used to define the length of the returned sequence.  

Example 1:

#This code will return a random element from the list.

import random

name_list = ['Bhumi' ,'Bhuvi' , 'Chotu' ,'Komal']

x= random.choices(name_list)

print(x)

#output: ['Bhuvi']

   

 Example 2:

#this code will return 10 random elements from the list.

import random

name_list = ['Bhumi' ,'Bhuvi' , 'Chotu' ,'Komal']

x= random.choices(name_list, k=10 )

print(x)

#output: ['Bhuvi', 'Chotu', 'Chotu', 'Chotu', 'Bhuvi', 'Chotu', 'Bhuvi', 'Bhuvi', 'Bhumi', 'Bhuvi']

6. sample():

sample() and choices() are similar to each other but the only difference is that choices() return multiple items from any sequence like list, tuple, or string with a replacement whereas sample() returns multiple items from any sequence like list, tuple, or string without any replacement.

This function returns a particular length of the sequence.

Syntax:

random.sample(sequence, k)

Parameter :

sequence: list, tuple, or string

k: It specifies the length of the returned sequence and it is an integer value.

Returns a new sequence of length k 

For Example: 

#this code will return a new list of length 2 as k=2

import random

name_list = ['Bharat' ,'Bhuvika' , 'Chutki' ,'Komolika']

x= random.sample(name_list, k=2 )

print(x)

#output: ['Bharat', 'Chutki']

  

7. seed(): 

   The seed function is used to save the state of a random function. It helps to generate the same random numbers on multiple executions of the code on the same as well as different machines for a specific seed value. It can be used to generate the same number every time with the same seed value.

Syntax:

random.seed()

For Example: 

#this python code demonstrates how we can use seed() function to generate the same number again and again.

import random

random.seed(40)

print("First number is ", random.randint(30,50))

random.seed(40)

print("Second number is ", random.randint(30,50))

#output:

# First number is 44

#Second number is 44


8. shuffle():

    shuffle() method is used to reorganize the order of the list items. That is it shuffle a list. It takes a sequence as an argument, the sequence can be a list, tuple, or a string. And reorganize the order of the items.

It returns the original sequence i.e list/tuple/string not a new sequence(list/tuple/string).

Syntax:

random.shuffle(sequence, function)

Parameters

sequence: It is mandatory & it can be a list, a tuple, or a string.

function: It is an optional argument, the name of the function that returns a number in the range [0.0,1.0). if not specified, the function random() will be used.

Example 1: This demonstrates the working of the shuffle() function with mandatory sequence argument and default function i.e random.random().

#this code will return the same list after shuffling the list.

import random

name_list = ['Bharat' ,'Bhuvika' , 'Chutki' ,'Komolika']

random.shuffle(name_list)

print(name_list)

#output: ['Bharat', 'Chutki', 'Komolika', 'Bhuvika']





Example 2: This demonstrates the working of the shuffle() function with mandatory sequence argument and user-defined function.

#This code will return the same list after shuffling the list.

import random

def ranfun():

  return 0.1


name_list = ['Bharat' ,'Bhuvika' , 'Chutki' ,'Komolika']

random.shuffle(name_list, ranfun)

print(name_list)

#output: ['Bhuvika', 'Chutki', 'Komolika', 'Bharat']

9. uniform():

 This method returns a random floating number between the specified numbers in the range [a,b].

Syntax:

random.uniform(a: int, b: int)

 Parameters: both are mandatory and included, a is the lowest possible outcome and b is the highest possible outcome.

It returns a random floating number between the two specified

Numbers, both numbers are included.

For Example:

#this python code demonstrates how we can use the uniform() function to generate random

# float number between the specified numbers(including both)

import random

x= random.uniform(40, 70)

print(x)

#output:66.41477037969156

10. triangular():

triangular() method returns a random floating number between two specified numbers(both included) but closer to the lowest possible outcome.

Syntax:

random.triangular(a , b, mode)

Parameter:

Note: here all three parameters are optional. 

 a is optional and the lowest possible outcome, default =0.

 b is optional and the highest possible outcome, default =1.

 the mode is optional and can be a number to weigh the result in any direction, by default it is the midpoint between a and b.

Example 1:

#This python code demonstrates how we can use the triangular() function to return  

# random floating number, with the lowest and highest possible outcomes.

import random

x= random.triangular(40, 70)

print(x)

#output:52.371493374442984

Example 2:

#this python code demonstrates how we can use the triangular() function to return a# random floating number with the lowest, highest possible outcomes and mode.

import random

x= random.triangular(40, 70, 50)

print(x)

#output: 59.40196245635055

Thanks for reading this article, I just hope you like it. Stay tuned for more articles like this. Don’t forget to comment and share content.

Till now, 

Happy Learning

MCQ

Related Article