python隨機數,隨機選擇……random
阿新 • • 發佈:2017-11-16
clas ack 排序 amp dom choice nts imp clu
import random from random import random, uniform, randint, randrange, choice, sample, shuffle list = [‘jack‘, ‘rose‘, ‘tom‘, ‘jerry‘] print(random()) # 隨機小數 0.0 <= x < 1.0 print(uniform(1, 10)) # 隨機小數 x, 1.0 <= x < 10.0 print(randint(1, 10)) # 隨機整數 1 to 10, endpoints included print(randrange(0, 101, 2)) # 隨機0-101,步進2,也就是偶數 print(randrange(0, 101, 5)) # 隨機0-101,步進5,也就是5的倍數 print(choice(‘abcdefghij‘)) # 隨機字符串中的一個字符 items = [1, 2, 3, 4, 5, 6, 7] print(items) shuffle(items) # 重新隨機排序 print(items) print(sample([1, 2, 3, 4, 5], 3)) # 隨機選3個
python隨機數,隨機選擇……random