Friday, June 9, 2023

The random numbers

To work with random numbers you will have to import random module.

The following functions can be used:

import random

#Random choice

print (" Working with Random Choice")

seq=[8,3,5,2,1,90,45,23,12,54]

print ("select randomly from ", seq," : ",random.choice(seq))

print("*******************")

#randomly select from a range

print ("randomly generate a number between 1 and 10 :

",random.randrange(1, 10))

print("*******************")

#random()

print ("randomly display a float value between 0 and 1 :

",random.random())

print("* * * * * *")

#shuffle elements of a list or a tuple

seq=[1,32,14,65,6,75]

print("shuffle ",seq,"to produce : ",random.shuffle(seq))

#uniform function to generate a random float number between two numbers

print ("randomly display a float value between 65 and 71 :

",random.uniform(65,71))

OUTPUT

Working with Random Choice

select randomly from [8, 3, 5, 2, 1, 90, 45, 23, 12, 54] : 2

*******************

randomly generate a number between 1 and 10 : 8

*******************

randomly display a float value between 0 and 1 :

0.3339711273144338

* * * * * *

shuffle [1, 32, 14, 75, 65, 6] to produce : None

randomly display a float value between 65 and 71 :

65.9247420528493 

Share:

0 comments:

Post a Comment