# python random and seed tutorial

## Random and seed

``````import numpy as np
import random
``````
``````# random.seed(1)
# np.random.seed(1)
#只要seed一样，不管运行多少次，每次产生的随机数都一样。
np.random.seed(1)
print np.random.rand(2,3)
print np.random.rand(3,1)
``````
``````[[  4.17022005e-01   7.20324493e-01   1.14374817e-04]
[  3.02332573e-01   1.46755891e-01   9.23385948e-02]]
[[ 0.18626021]
[ 0.34556073]
[ 0.39676747]]
``````
``````#只要seed一样，不管运行多少次，每次产生的随机数都一样。
np.random.seed(1)
print np.random.rand(2,3)
print np.random.rand(3,1)
``````
``````[[  4.17022005e-01   7.20324493e-01   1.14374817e-04]
[  3.02332573e-01   1.46755891e-01   9.23385948e-02]]
[[ 0.18626021]
[ 0.34556073]
[ 0.39676747]]
``````
``````import random
random.seed(1)
random.random()
``````
``````0.13436424411240122
``````
``````random.seed(1)
random.random()
``````
``````0.13436424411240122
``````
``````np.random.seed(1)
np.random.randint(0,6, size=(4,5))
``````
``````array([[5, 3, 4, 0, 1],
[3, 5, 0, 0, 1],
[4, 5, 4, 1, 2],
[4, 5, 2, 4, 3]])
``````
``````np.random.seed(1)
np.random.randint(0,6, size=(4,5))
``````
``````array([[5, 3, 4, 0, 1],
[3, 5, 0, 0, 1],
[4, 5, 4, 1, 2],
[4, 5, 2, 4, 3]])
``````

## History

• 20180806: created.

Author: kezunlin
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source kezunlin !

TOC