Python Random module

Python Random module

The Python Random module is a built-in module to generate the pseudo-random variables. It can be used perform some action randomly such as to get a random number, selecting a random elements from a list, shuffle elements randomly, etc.

The Python random module functions depend on a pseudo-random number generator function random(), which generates the float number between 0.0 and 1.0.

This module defines several functions to generate random numbers.

We can use these functions while developing games, in cryptography and to generate random numbers on fly for authentication.

random() Function:

This function always generate some float value between 0 and 1 ( not inclusive)

0<x<1

 

from random import *

for i in range(10):

print(random())

Output

0.4572685609302056

0.6584325233197768

0.15444034016553587

0.18351427005232201

0.1330257265904884

0.9291139798071045

0.6586741197891783

0.8901649834019002

0.25540891083913053

0.7290504335962871

randint() Function:

To generate random integer between two given numbers(inclusive)

from random import *

for i in range(10):

print(randint(1,100)) # generate random int value between 1 and 100(inclusive)

Output

51

44

39

70

49

74

52

10

40

8

random.choice()

This function returns a randomly selected element from a non-empty sequence.

# importing “random”  module.

import random

# We are using the choice() function to generate a random number from

# the given list of numbers.

print (“The random number from list is : “,end=””)

print (random.choice([50, 41, 84, 40, 31]))

 

OUTPUT:

The random number from list is : 84

random.shuffle()

This function randomly reorders the elements in the list.

random.randrange(beg,end,step)

This function is used to generate a number within the range specified in its argument. It accepts three arguments, beginning number, last number, and step, which is used to skip a number in the range. Consider the following example.

import random

print (“A random number from range is : “,end=””)

print (random.randrange(100, 500, 10))

OUTPUT:

A random number from range is : 290

random.seed()

This function is used to apply on the particular random number with the seed argument. It returns the mapper value

# importing “random” module.

import random

# using random() to generate a random number

# between 0 and 1

print(“The random number between 0 and 1 is : “, end=””)

print(random.random())

 

# using seed() to seed a random number

random.seed(4)

OUTPUT:

The random number between 0 and 1 is : 0.4405576668981033

uniform() Function:

It returns random float values between 2 given numbers (not inclusive)

from random import *

for i in range(10):

print(uniform(1,10))

 

Output

9.787695398230332

6.81102218793548

8.068672144377329

8.567976357239834

6.363511674803802

2.176137584071641

4.822867939432386

6.0801725149678445

7.508457735544763

1.9982221862917555

choice() Function:

It won’t return random number.

It will return a random object from the given list or tuple.

from random import *

list=[“Sunny”,”Bunny”,”Chinny”,”Vinny”,”pinny”]

for i in range(10):

print(choice(list))

OUTPUT:

Bunny

pinny

Bunny

Sunny

Bunny

pinny

pinny

Vinny

Bunny

Sunny

Related Posts:

Python Tutorial – Learn Python

What is Python? What makes Python so Powerful?

Variables in Python – Constant, Global & Static Variables

Namespacing and Scopes in Python

Operators in Python

Data Types in Python

STRING Data Type in Python

LIST Data Structure in PYTHON

TUPLE Data Structure in PYTHON

Differences between List and Tuple

SET Data Structure in PYTHON

DICTIONARY Data Structure in PYTHON

Database Programming in Python

What is Multithreading in Python?

Python Exception Handling Using try, except and finally statement

File Handling in Python

Python reduce() Function

Python map() Function

Python filter() Function

Lambda Function in PYTHON (Anonymous Function)

Python Interview Question And Answers