Python定义了一组用于生成或操纵随机数的函数。这种特殊类型的功能用于许多游戏,彩票或需要随机数生成的任何应用程序中。
随机数运算:
1.choice():此函数用于从容器生成1个随机数。
2.randrange(beg, end, step):此函数用于在其参数指定的范围内生成随机数。此函数有3个参数,开始编号(包含在生成中),最后一个编号(不包含在生成中)和step(在选择时跳过范围内的数字)。
# Python code to demonstrate the working of # choice() and randrange() # importing "random" for random operations import random # using choice() to generate a random number from a # given list of numbers. print ("A random number from list is : ",end="") print (random.choice([1, 4, 8, 10, 3])) # using randrange() to generate in range from 20 # to 50. The last parameter 3 is step size to skip # three numbers when selecting. print ("A random number from range is : ",end="") print (random.randrange(20, 50, 3))
输出:
A random number from list is : 4A random number from range is : 41
3.random() :此数字用于生成小于1且大于或等于0的float随机数。
4.seed():此函数使用提到的seed参数映射特定的随机数。在种子值之后调用的所有随机数都返回映射的数字。
# Python code to demonstrate the working of # random() and seed() # importing "random" for random operations import random # using random() to generate a random number # between 0 and 1 print ("A random number between 0 and 1 is : ", end="") print (random.random()) # using seed() to seed a random number random.seed(5) # printing mapped random number print ("The mapped random number with 5 is : ", end="") print (random.random()) # using seed() to seed different random number random.seed(7) # printing mapped random number print ("The mapped random number with 7 is : ", end="") print (random.random()) # using seed() to seed to 5 again random.seed(5) # printing mapped random number print ("The mapped random number with 5 is : ",end="") print (random.random()) # using seed() to seed to 7 again random.seed(7) # printing mapped random number print ("The mapped random number with 7 is : ",end="") print (random.random())
输出:
A random number between 0 and 1 is : 0.510721762520941The mapped random number with 5 is : 0.6229016948897019The mapped random number with 7 is : 0.32383276483316237The mapped random number with 5 is : 0.6229016948897019The mapped random number with 7 is : 0.32383276483316237
5.shuffle():此函数用于随机排列整个列表。
6.uniform(a, b) :此函数用于在其参数中提到的数字之间生成浮点随机数。它包含两个参数:下限(包含在生成中)和上限(不包含在生成中)。
# Python code to demonstrate the working of # shuffle() and uniform() # importing "random" for random operations import random # Initializing list li = [1, 4, 5, 10, 2] # Printing list before shuffling print ("The list before shuffling is : ", end="") for i in range(0, len(li)): print (li[i], end=" ") print("") # using shuffle() to shuffle the list random.shuffle(li) # Printing list after shuffling print ("The list after shuffling is : ", end="") for i in range(0, len(li)): print (li[i], end=" ") print("") # using uniform() to generate random floating number in range # prints number between 5 and 10 print ("The random floating point number between 5 and 10 is : ",end="") print (random.uniform(5,10))
输出:
The list before shuffling is : 1 4 5 10 2 The list after shuffling is : 2 1 4 5 10 The random floating point number between 5 and 10 is : 5.183697823553464