1. 程式人生 > >python基礎教程_學習筆記16:標準庫:一些最愛——random

python基礎教程_學習筆記16:標準庫:一些最愛——random

標準庫:一些最愛

random

random模組包括返回隨機數的函式,可以用於模擬或者用於任何產生隨機輸出的程式。

事實上,所產生的數字都是偽隨機數,它們以一個可預測的系統作為基礎,除非是為了強加密的目標,否則這些隨機數還是足夠隨機的。如果真的需要隨機性,可以使用os模組的urandom函式。

重要函式

函式

描述

random()

返回0<=n<1之間的隨機實數n,其中0<n<=1

getrandbits(n)

以長整型形式返回n個隨機位(二進位制數)

uniform(a,b)

返回隨機實數n,其中a<=n<b

randrange([start],stop,[step])

返回range(start,stop,step)中的隨機數

choice(seq)

從序列seq中返回隨意元素

shuffle(seq[,random])

原地指定序列seq

sample(seq,n)

從序列seq中選擇n個隨機且獨立的元素

random.getrandbits以長整型形式返回給定的位數(二進位制數)。如果處理的是真正的隨機事務(比如加密),這個函式尤為有用。

>>> import random

>>> random.random()

0.21811083216655824

>>> random.getrandbits(2)

3L

>>> random.getrandbits(2)

3L

>>> random.getrandbits(2)

0L

>>> random.uniform(1,4)

3.511648764533735

>>> random.uniform(1,9)

6.00895159575027

>>> random.randrange(5,100,21)

5

>>> random.randrange(5,100,21)

26

>>> random.randrange(5,100,21)

68

>>> random.randrange(5,100,21)

47

>>> random.randrange(10)

4

>>> random.randrange(10)

3

>>> random.choice((5,6,9))

6

>>> random.choice([5,6,9])

6

>>> random.choice('signjing')

'j'

因為random.shuffle函式原地移位序列,因此要求序列一定是可變的。

>>> a=['a','b','c']

>>> random.shuffle(a)

>>> a

['b', 'a', 'c']

>>> random.shuffle(a)

>>> a

['c', 'b', 'a']

>>> random.sample(a,2)

['a', 'c']

>>> random.sample(a,2)

['c', 'b']

練習 獲取隨機數

指令碼內容

$ cat random-example-1.py

#File : random-example-1.py

import random

for i in range(5):

        print random.random(),

        print random.uniform(10,20),

        print random.randint(100,1000),

        print random.randrange(100,1000,2)

執行結果

$ python random-example-1.py

0.370761541392 17.2201292256 839 342

0.808063846391 12.0582042123 224 910

0.855181475123 13.6575143789 406 510

0.713555346237 14.0263174291 306 698

0.637399405203 13.3270555599 799 540

練習 隨機序列中的元素

指令碼內容

$ cat random-example-2.py

#File : random-example-2.py

import random

for i in range(5):

        print random.choice([1,2,3,5,9])

執行結果

$ python random-example-2.py

2

2

2

5

9

練習 洗牌

指令碼內容

$ cat random-example-4.py

#File : random-example-4.py

import random

try:

        shuffle=random.shuffle

except AttributeError:

        def shuffle(x):

                for i in xrange(len(x)-1,0,-1):

                        j=int(random.random()*(i+1))

                        x[i],x[j]=x[j],x[i]

cards=range(52)

shuffle(cards)

myhand=cards[:5]

print myhand

執行結果

$ python random-example-4.py

[20, 44, 17, 14, 0]

$ python random-example-4.py

[8, 17, 27, 13, 42]

練習 高斯隨機數

指令碼內容

$ cat random-example-3.py

#File : random-example-3.py

import random

histogram=[0]*20

for i in range(25):

        i=int(random.gauss(5,1)*2)

        histogram[i]=histogram[i]+1

m=max(histogram)

for v in histogram:

        print "*" * (v * 50 / m)

執行結果

$ python random-example-3.py

******

******

*************************

*******************************

**************************************************

************

******************

******