您现在的位置是:课程教程文章
python如何生成随机序列?
2023-12-18 18:31课程教程文章 人已围观
生成随机序列的两种方式:
1、生成内容不重复的随机序列
>>> import random >>> s = [x for x in range(0, 10)] >>> s [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> random.shuffle(s) >>> s [8, 4, 1, 5, 2, 0, 7, 6, 9, 3]
2、用random生成随机序列
# 生成一个【0,10】长度为100的随机序列 >>> random_int_list = [] >>> for _ in range(100): ... random_int_list.append(random.randint(0, 10)) ... >>> random_int_list [5, 4, 8, 0, 5, 3, 7, 7, 9, 10, 0, 8, 9, 5, 3, 9, 2, 9, 7, 5, 4, 6, 3, 1, 10, 10, 6, 10, 7, 8, 0, 10, 7, 8, 0, 9, 2, 1, 10, 6, 4, 10, 4, 3, 10, 4, 5, 7, 6, 10, 7, 5, 4, 4, 2, 7, 2, 3, 3, 1, 10, 10, 3, 2, 7, 8, 2, 0, 1, 4, 10, 9, 4, 10, 2, 6, 7, 10, 0, 5, 4, 0, 4, 10, 0, 5, 1, 3, 6, 6, 3, 0, 0, 5, 2, 9, 7, 3, 3, 9] >>>课程教程:python如何生成随机序列?
下一篇:没有了