[3-1] 必要なモジュールをインポートします。
In [1]:
import numpy as np
import matplotlib.pyplot as plt
from numpy.random import randint
[3-2] 1回分のゲームをシミュレーションする関数を用意します。手持ちのポイントと賭けるポイントを渡すと、ゲームの結果に応じて、増減した後の手持ちのポイントが返ります。
In [2]:
def coin_game(money, bet):
coin = randint(2)
if coin == 0:
money += bet
else:
money -= bet
return money
[3-3] 毎回10ポイント賭けるという戦略で、100回分のゲームを繰り返して、手持ちのポイントの変動をグラフ表示します。
In [3]:
money = 1000
result = []
for i in range(100):
money = coin_game(money, 10)
result.append(money)
fig = plt.figure(figsize=(5,3))
subplot = fig.add_subplot(1,1,1)
subplot.plot(range(len(result)), result)
Out[3]:
[3-4] 毎回手持ちのポイントの半分を賭けるという戦略で、100回分のゲームを繰り返して、手持ちのポイントの変動をグラフ表示します。
In [4]:
money = 1000
result = []
for i in range(100):
result.append(money)
money = coin_game(money, money/2)
fig = plt.figure(figsize=(5,3))
subplot = fig.add_subplot(1,1,1)
subplot.plot(range(len(result)), result)
Out[4]: