Python核心编程 取随机数
《Python核心编程》 5.17生成一个具有N个元素的由随机数n组成的列表,其中N范围为1--100,n范围为1--2**32-1,再从该列表里随机挑选出N个元素出来,排序,显示
解答如下:
import random;
randomList=[];
subRandomList=[];
maxValue=2**31-1;
index=0;
number=5;
def createRandomList():
for i in range(number) :
randomList.append(random.randint(1,maxValue));
def getSubRandomList():
for i in range(number) :
index=random.randint(0,len(randomList)-1);
subRandomList.append(randomList);
createRandomList();
print "before sublist:",randomList;
getSubRandomList();
subRandomList.sort();
print "after sublist:",subRandomList;
输出结果如下所示:
before sublist:
after sublist:
页:
[1]