1〜20までの乱数を重複しないように20個表示するスクリプトを考えてみた。
30回表示します。
#random_num3.py
import random
for i in range(30):
a=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
b=[]
while len(a)>1:
index=int(random.random()*len(a)+1)
b.append(a[index-1])
del a[index-1]
b.append(a[0])
print b
表示結果
C:\Python25>random_num3.py
[11, 5, 15, 18, 6, 13, 16, 9, 8, 14, 1, 4, 7, 10, 12, 3, 20, 2, 19, 17]
[17, 9, 12, 15, 7, 2, 18, 5, 16, 6, 4, 10, 19, 14, 1, 3, 20, 8, 13, 11]
[13, 17, 1, 14, 19, 11, 2, 4, 10, 15, 3, 5, 18, 7, 20, 6, 8, 16, 12, 9]
[6, 12, 19, 10, 17, 5, 13, 4, 1, 2, 3, 20, 16, 11, 14, 8, 18, 7, 15, 9]
[12, 2, 15, 5, 19, 3, 6, 9, 20, 7, 4, 10, 17, 8, 14, 16, 13, 18, 1, 11]
[18, 5, 2, 20, 19, 10, 7, 15, 16, 12, 6, 4, 11, 1, 3, 13, 8, 9, 17, 14]
[6, 19, 14, 13, 15, 8, 5, 7, 11, 10, 2, 4, 12, 16, 20, 18, 3, 9, 1, 17]
[4, 14, 13, 9, 2, 19, 12, 5, 11, 17, 8, 10, 16, 6, 18, 7, 1, 20, 3, 15]
[18, 10, 20, 12, 16, 7, 17, 2, 6, 9, 8, 19, 11, 3, 15, 1, 14, 13, 5, 4]
[14, 1, 15, 5, 11, 13, 2, 7, 4, 17, 6, 3, 18, 16, 10, 9, 8, 19, 12, 20]
[13, 18, 19, 9, 7, 3, 20, 5, 2, 11, 15, 14, 8, 6, 10, 17, 16, 4, 12, 1]
[14, 15, 20, 9, 16, 1, 2, 10, 18, 3, 13, 17, 5, 12, 19, 4, 8, 7, 11, 6]
[13, 6, 3, 10, 5, 11, 14, 2, 16, 19, 20, 4, 8, 18, 1, 7, 12, 17, 15, 9]
[17, 7, 19, 12, 2, 20, 3, 15, 16, 5, 10, 14, 1, 9, 18, 8, 6, 11, 13, 4]
[3, 14, 11, 20, 18, 2, 17, 8, 7, 13, 6, 12, 5, 10, 15, 19, 9, 16, 1, 4]
[11, 20, 4, 15, 1, 19, 18, 8, 3, 14, 2, 13, 6, 12, 7, 5, 9, 17, 16, 10]
[8, 7, 18, 9, 11, 4, 20, 15, 5, 10, 16, 1, 12, 14, 13, 19, 3, 2, 6, 17]
[15, 20, 6, 4, 8, 16, 2, 18, 13, 19, 11, 9, 7, 3, 10, 12, 17, 1, 5, 14]
[8, 3, 11, 20, 9, 18, 7, 1, 13, 5, 4, 19, 2, 15, 12, 14, 17, 10, 16, 6]
[2, 6, 10, 12, 8, 14, 15, 18, 3, 19, 13, 20, 4, 16, 17, 11, 5, 1, 9, 7]
[11, 2, 8, 15, 17, 5, 3, 20, 1, 10, 13, 18, 19, 12, 14, 4, 7, 16, 6, 9]
[19, 20, 18, 14, 12, 16, 9, 17, 11, 10, 5, 1, 2, 3, 13, 7, 4, 15, 8, 6]
[14, 8, 2, 15, 11, 13, 9, 7, 16, 19, 17, 5, 6, 12, 18, 1, 4, 20, 10, 3]
[4, 16, 3, 9, 6, 12, 8, 5, 17, 1, 14, 7, 18, 11, 19, 10, 15, 13, 20, 2]
[2, 19, 8, 9, 12, 4, 5, 20, 10, 13, 11, 18, 7, 14, 16, 3, 6, 17, 15, 1]
[11, 5, 1, 10, 3, 8, 18, 19, 15, 13, 12, 16, 7, 2, 4, 20, 6, 9, 17, 14]
[16, 19, 15, 6, 9, 14, 5, 2, 12, 8, 10, 18, 3, 13, 4, 7, 1, 11, 20, 17]
[8, 6, 13, 16, 7, 1, 4, 19, 5, 15, 11, 12, 14, 18, 20, 3, 2, 17, 10, 9]
[4, 17, 18, 10, 6, 16, 13, 7, 1, 3, 11, 9, 14, 5, 12, 2, 20, 19, 15, 8]
[16, 10, 6, 5, 20, 13, 2, 11, 9, 18, 1, 14, 3, 17, 12, 4, 19, 15, 8, 7]
常に勉強します。
- -
- -