[1-1] 整数の乱数を発生させるモジュール randint をインポートします。


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

[1-2] 1〜6の乱数を10個発生させる例です。


In [2]:
print randint(1,7,10)


[1 1 2 4 2 4 4 3 2 1]

[1-3] 1〜6の乱数を1000個発生したものを変数 dice に格納します。


In [3]:
dice = randint(1,7,1000)

[1-4] 先ほどの乱数をヒストグラムに表示します。


In [4]:
fig = plt.figure(figsize=(3,3))
subplot = fig.add_subplot(1,1,1)
subplot.set_xlim(0.5,6.5)
subplot.hist(dice, bins=np.linspace(0.5,6.5,7))


Out[4]:
(array([ 167.,  173.,  177.,  142.,  152.,  189.]),
 array([ 0.5,  1.5,  2.5,  3.5,  4.5,  5.5,  6.5]),
 <a list of 6 Patch objects>)

[1-5] 2個のサイコロ振った合計を1000回分計算して、ヒストグラムに表示します。


In [5]:
dice1 = randint(1,7,1000)
dice2 = randint(1,7,1000)
dice_total = dice1 + dice2

fig = plt.figure(figsize=(5,3))
subplot = fig.add_subplot(1,1,1)
subplot.set_xlim(1.5,12.5)
subplot.hist(dice_total, bins=np.linspace(1.5,12.5,12))


Out[5]:
(array([  27.,   43.,   78.,   94.,  155.,  167.,  152.,  102.,   95.,
          59.,   28.]),
 array([  1.5,   2.5,   3.5,   4.5,   5.5,   6.5,   7.5,   8.5,   9.5,
         10.5,  11.5,  12.5]),
 <a list of 11 Patch objects>)