random 用法
1 import random
2
3 random.seed(37)
4
5 print(random.random()) # 0.6820045605879779
6 print(random.random()) # 0.09160260807956389
7 print(random.random()) # 0.6178163488614024
Rolling dice - randrange
1 import random
2
3 print( 1 + int( 6 * random.random() ))
4
5 print(random.randrange(1, 7))
6
7 # Oneof the following: 1, 2, 3, 4, 5, 6
Random choice
1 import random
2
3 letter = "abcdefghijklmno"
4 print(random.choice(letters)) # pick one of the letters
5
6 fruits = ["Apple", "Banana", "Peach", "Orange", "Durian", "Papaya"]
7 print(random.choice(fruits))
8 # pick one of the fruits