[5-1] 必要なモジュールをインポートします。


In [1]:
import numpy as np
import matplotlib.pyplot as plt
from numpy.random import random
from numpy.random import normal

[5-2] randomを用いて、0〜1の範囲の乱数を5個発生します。


In [2]:
print random(5)


[ 0.8213769   0.8659264   0.08605054  0.32675946  0.31133611]

[5-3] 100〜150の範囲の乱数を5個発生します。


In [3]:
print 100+random(5)*50


[ 106.24571699  140.5191746   103.23735344  121.25694568  117.07591235]

[5-4] normalを用いて、正規分布の乱数を発生します。ここでは、標準偏差1の場合と標準偏差2の場合をグラフで比較しています。


In [4]:
fig = plt.figure(figsize=(5,6))

subplot = fig.add_subplot(2,1,1)
data = normal(5,1,1000)
subplot.hist(data, bins=np.linspace(0,10,20))

subplot = fig.add_subplot(2,1,2)
data = normal(5,2,1000)
subplot.hist(data, bins=np.linspace(0,10,20))


Out[4]:
(array([   3.,   17.,   24.,   28.,   47.,   69.,   67.,   87.,   90.,
         115.,  109.,   76.,   74.,   69.,   39.,   33.,   27.,   11.,    4.]),
 array([  0.        ,   0.52631579,   1.05263158,   1.57894737,
          2.10526316,   2.63157895,   3.15789474,   3.68421053,
          4.21052632,   4.73684211,   5.26315789,   5.78947368,
          6.31578947,   6.84210526,   7.36842105,   7.89473684,
          8.42105263,   8.94736842,   9.47368421,  10.        ]),
 <a list of 19 Patch objects>)