1. 列表排序可採用兩種方法html
sorted(list) 直接改變listpython
調用list的方法 list.sortapp
2.dom
random.randint(a,b) 生成大於等於a小於等於b的整數htm
random.random() 生成一個在[0,1)區間上的實數blog
random.choice(sequence) sequence泛指list、tuple、字符串等排序
random.randrange(start,stop,step) step務必給出,在本次實踐中,若是不給出將會隨機給一個值且極大可能爲負數字符串
具體參考 https://www.cnblogs.com/whiteprism/p/6290814.htmlit
3. 代碼實例class
導入模塊 import random
def FetchRandom(): '生成隨機數列表並從中抓取' N1 = random.randint(2,100) N2 = random.randint(1,100) while N2>N1: N2 = random.randint(1,100) alist = [] for i in range(N1): n = random.randint(0, 2 ^ 31 - 1) alist.append(n) blist = [] for i in range(N2): blist.append(random.randrange(alist[0],alist[N1-1],1))
#blist.append(random.choice(alist)) print sorted(blist) print 'N1=',N1,' N2=',N2,' n=',n
4. 輸出效果