[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-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]:
[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]: