random模块通常用来生成随机数,结合time模块生成随机数的代码:
import time
import random
random.seed(time.time())
x = random.randint(0,9)
x是[0,9]的整数,注意是闭区间
random.sample的用法,可返回从序列中选择的项目的特定长度列表,即列表,元组,字符串或集合。用于随机抽样而无需更换。多用于截取列表的指定长度的随机数,但是不会改变列表本身的排序:
random.sample(range(0, 10), 8)
[6, 4, 7, 3, 5, 0, 9, 1]
randomList = [1,2,3,4,5,6,7,8,9,10]
random.sample(randomList, 8)
[4, 9, 6, 8, 1, 10, 2, 5]
Original: https://www.cnblogs.com/camilia/p/16409077.html
Author: CAMILIA
Title: [python]-random模块-手动随机数
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/683345/
转载文章受原作者版权保护。转载请注明原作者出处!