import random from random import random, uniform, randint, randrange, choice, sample, shuffle print(random()) # 0~1之間隨機選取一個數 print(uniform(1, 10)) # 1~10之間隨機選取一個數,可以是小數 print(randint(1, 10)) # 1~10之間隨機選取一個整數 print(randrange(0, 101, 2)) # 0~101之間隨機選取一個2的倍數 print(randrange(0, 101, 5)) # 0~101之間隨機選取一個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個,無放回選取