Working with Random Module

Working with Random Module

  • 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. 

1. Random() Function

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

Eg:

2. Randint() Function

  • To generate random integer between two given numbers(inclusive)

Eg:

3. uniform()

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

Eg:

  • random() ===>in between 0 and 1 (not inclusive) 
  • randint(x,y)   ==>in between x and y ( inclusive) 
  • uniform(x,y)   ==>  in between x and y ( not inclusive) 

4. randrange([start],stop,[step])

  • returns a random number from range      
  • start<= x < stop      
  • start argument is optional and default value is 0      
  • step argument is optional and default value is 1
  1. randrange(10)-->generates a number from 0 to 9 
  2. randrange(1,11)-->generates a number from 1 to 10 
  3. randrange(1,11,2)-->generates a number from 1,3,5,7,9 

Eg:

Eg:

Eg:

5. choice() function

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

Eg:




 

Comments