this helps to cover all major topics and questions
from python language and helps you improve your coding capabilities in python programming
Wednesday, October 28, 2020
Random module
Random Module
Dear Readers,todays post is on random module Random Module Usage in PythonImporting necessary libraries from random module
fromrandomimport random,gauss,randrange,randint,sample,choice,choices,seed
# Use seed to get same random number again and again as the output, without seeding you would get varied output everytime you run the program.
seed(9)
# for getting a random floating number always less than oneprint(random())
# for getting intergers we use randint and randrange ,randint(start_value,stop_value) and randrange(start_value,stop_value)print(randint(0,9))
print(randrange(0,1000))
# get a random choice out of a list or a tuple using choice attribute , you can use choices to get a random sample as an output with specified size
list1 = ['a','b','c','d','e','f','g','h']
print(choice(list1))
print(choices(list1,k=4)) # here k means size of the output/or number of random selections to be made and ouput is in list format# to get a gaussian number we use , gauss(mean,standard deviation) ['THIS IS OPTIONAL'] print(gauss(10,2.5))
# like random choices we can use random sample, like sample(list_name,size)
list2 = [1,2,3,4,5,6,7]
print(sample(list2,4)) # outputs list format only