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[index]);
createRandomList();
print "before sublist:",randomList;
getSubRandomList();
subRandomList.sort();
print "after sublist:",subRandomList;
    


输出结果如下所示:
before sublist: [1366810753, 1451317925, 1454193159, 1803178957, 1680453590]
after sublist: [1366810753, 1451317925, 1451317925, 1454193159, 1680453590]

你可能感兴趣的:(编程,python)