Random numbers don't understand? They don't exist!

Posted by bdee1 on Sun, 08 Dec 2019 04:56:21 +0100

python random number

Python defines a set of functions for generating or manipulating random numbers. This particular type of function is used in many games, lotteries, or any application that needs to generate random numbers.

Operation of random number

1. choice(): this function is used to generate a random number in the container

2. Random (beg, end, step): this function is used to generate random numbers in a given parameter. This function takes three parameters: start value, end value and skip value.

Here is the sample code:

#Import 'random' module for random operation
import random

#Use choice() to randomly select a value from the given list

print('A random number from list is :',end='')
print(random.choice([1,2,3,4,5,6,7]))


#Use the random() function to generate a random number in a given range.

print('A random number from range is :',end='')
print(random.randrange(20,50,3))

3. random(): used to generate a random floating-point number less than 1, greater than or equal to 0

4. seed(): this function maps a specific random number to the seed parameter mentioned earlier. All random numbers that are called after seed values return the number of mapped.

import random
#Output floating point number
print ("A random number between 0 and 1 is : ", end="") 
print (random.random()) 
  
#Using seed() to generate a random seed 
random.seed(5) 
  
# Random number of print map
print ("The mapped random number with 5 is : ", end="") 
print (random.random()) 
  
# Generate another random seed
random.seed(7) 
  
print ("The mapped random number with 7 is : ", end="") 
print (random.random()) 
  
#Again, use 5 as a parameter
random.seed(5) 
  

print ("The mapped random number with 5 is : ",end="") 
print (random.random()) 
  
#Again, use 7 as a parameter 
random.seed(7) 
  

print ("The mapped random number with 7 is : ",end="") 
print (random.random()) 

Output:

A random number between 0 and 1 is : 0.510721762520941
The mapped random number with 5 is : 0.6229016948897019
The mapped random number with 7 is : 0.32383276483316237
The mapped random number with 5 is : 0.6229016948897019
The mapped random number with 7 is : 0.32383276483316237

5. shuffle(): used to scramble and rearrange the values in the whole list.

6. uniform(a,b): used to generate floating-point numbers in a given range

import random 

# Initialization list 
li = [1, 4, 5, 10, 2] 
  
# Print list before rearrangement 
print ("The list before shuffling is : ", end="") 
for i in range(0, len(li)): 
    print (li[i], end=" ") 
print("\r") 
  
# Remake 
random.shuffle(li) 
  
# Print the rearranged list
print ("The list after shuffling is : ", end="") 
for i in range(0, len(li)): 
    print (li[i], end=" ") 
print("\r") 

#Using uniform() to generate floating point numbers in a given range
print ("The random floating point number between 5 and 10 is : ",end="") 
print (random.uniform(5,10)) 

Output:

The list before shuffling is : 1 4 5 10 2 

The list after shuffling is : 10 1 4 2 5 

The random floating point number between 5 and 10 is : 5.923955296706723

Topics: Python less