python隨機數,隨機選擇……random


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個

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM