Python random module

random module in python

Random 

Python has huge collection of modules in which Random is one of it. Python Random module is used to generate random numbers. There are several functions under random module. To use random module we need to import that module using  import random. This module is very useful to create a simple games which requires random numbers. 

Here we will discuss on

  • Seed
  • Shuffle
  • random.random()
  • Randrange
  • Randint

At last we will play a simple game to understand the concept

SEED

Seed is used to fix the random number that to be generated. It generates same random number even if you execute it multiple times . Mainly used to encrypting key.

If you use seed then output of random function will be same even after several executions. Observe the difference between 3rd print statement and remaining statements.

import random
#Statement with seed
random.seed(3)
print(random.random())
random.seed(3)
print(random.random())

 

#statement without seed
print(random.random())

 

random.seed(3)
print(random.random())
0.23796462709189137
0.23796462709189137
0.5442292252959519
0.23796462709189137

Shuffle

Using shuffle function in random module shuffles the given list in random manner. Internally it swaps the data in the list randomly.  

import random
y=[1,2,3,4,5
print(“List items before shuffling :\n,y)
random.shuffle(y)        
print(“List items after shuffling :\n,y) 
List items before shuffling :
[1, 2, 3, 4, 5]
List items after shuffling :
[5, 2, 3, 4, 1]

random.random()

random function in Python random module generates a random number in range of  zero to one , which is floating number.

import random

print(random.random())
print(random.random())
print(random.random())
0.5442292252959519
0.36995516654807925
0.6039200385961945

Randrange

randrange function in random module generates a random value in given range

Syntax:  random.randrange(start,stop,incrementer) 
start- starting point of range (inclusive)
stop- ending point of range (exclusive)
incrementer- optional parameter which is defaultly 1.
first print statement prints random number from 10 to 19
second print statement prints random number from 1 to 9 and here increment value is 2 so it generates only odd random values.

import random

print(random.randrange(10,20))
print(random.randrange(1,10,2))
11
1

Randint

randint is also similar to randrange , but in randint both the given parameter is inclusive and without incremental value

Syntax: random.randint(start,stop)
start- starting point of range (inclusive)
stop- ending point of range (inclusive) 

import random

print(random.randint(10,20))

print(random.randint(1,10)) 
14
9

Guess The Number

Lets create a simple game where you need to predict the number that will be generated by the computer in the range 1 to 10.

import random
n=int(input(“Guess a number between 1 to 10 : “))
x=random.randint(1,10
print(“Computer generated number :”,x)
print(“Your guessed number :”,n)
if(n==x):
    print(“You guessed it right :)”)
else:
    print(“Wrong guess , Try again “)    

Output 1:

Guess a number between 1 to 10 : 5
Computer generated number : 1
Your guessed number : 5
Wrong guess , Try again

Output 2:

Guess a number between 1 to 10 : 4
Computer generated number : 4
Your guessed number : 4
You guessed it right :)