python如何生成随机序列?
发布时间:2026-01-16 17:27:29

生成随机序列的方法有两种:
1、生成内容不重复的随机序列
>>>importrandom >>>s=[xforxinrange(0,10)] >>>s [0,1,2,3,4,5,6,7,8] >>>random.shuffle(s) >>>s [8,4,1,5,2,7,6,9,3]
2、使用random生成随机序列
#随机序列生成长度为100的[0,10] >>>random_int_list=[] >>>for_inrange(100): ...random_int_list.append(random.randint(0,10)) ... >>>random_int_list 10,10,6,3,9,2,9,2,9,7,3,7,7,7,7,7,9,1,1,1,10,6,3,9,2,9,7,4,6,3,1, 10、7、8、0、10、7、8、0、9、2、106、10、4、10、4、5、7、6、10、7、4、4、4、4、 2、2、3、1、10、10、3、2、7、2、1、1、9、4、10、2、6、10、10、10、10、3、10、3、2、2、2、2、2、2、2、2、2、1、1、10、1、1、1、10、1、1、10、2、1、2、10、2、1、2、1、10、2、1、10、7、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、3、3、3、3、3、3、3、3、3、3、3、3、3、3、1、1、1、1、1、1、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、3、3、3、3、1、1、1、1、1、1、1、1、3、1、1、3、1、1、1、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、1、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、2、1、1、1、2、2、2、2、2、2、2、2、1、2、2、2、2、2、1、2、2、2、2、2、2、2、2、2、2、2、2、2、1、1、1、1、1、1、1、 0,4,10,0,5,1,3,6,3,0,0,5,2,9,7,3,3,3,9 >>>
