とあるシミュレーション

連休明けまでに書けばいいやと思っていた講義用のシミュレータが、2時間後の実験に必要であることが判明して、30分で書く。

import random

deck = range(2,10)
deck += [10] * 4
deck += [11]

deck *= 4

result = { j:{i:0 for i in range(17,23)} for j in range(2,12) }

print("Deck check")
print(deck)

for c in range(1000):
    random.shuffle(deck)
    hand = deck[-1]
    if deck[-1] == 11:
        hasa = 1
    else:
        hasa = 0
        
    for draw in deck:
        hand = hand + draw
        if draw == 11:
            hasa += 1
        if hand > 21 and hasa > 0:
            hand = hand - 10
            hasa -= 1
        if hand >= 17:
            break

    if hand>21:
        hand = 22

    result[deck[-1]][hand] += 1

print("Simulation Result")
for k, r in result.iteritems():
    print("%d : %s" % (k, r) )

実行結果

Simulation Result
2 : {17: 7, 18: 8, 19: 6, 20: 13, 21: 8, 22: 34}
3 : {17: 10, 18: 11, 19: 19, 20: 6, 21: 13, 22: 31}
4 : {17: 13, 18: 11, 19: 6, 20: 10, 21: 7, 22: 33}
5 : {17: 15, 18: 10, 19: 13, 20: 9, 21: 3, 22: 31}
6 : {17: 22, 18: 8, 19: 14, 20: 6, 21: 5, 22: 30}
7 : {17: 27, 18: 10, 19: 3, 20: 5, 21: 6, 22: 19}
8 : {17: 5, 18: 41, 19: 13, 20: 12, 21: 7, 22: 17}
9 : {17: 0, 18: 0, 19: 0, 20: 0, 21: 0, 22: 0}
10 : {17: 56, 18: 41, 19: 13, 20: 129, 21: 48, 22: 71}
11 : {17: 12, 18: 10, 19: 7, 20: 6, 21: 23, 22: 7}

This entry was posted in ソフトウェア, 教育. Bookmark the permalink.

One Response to とあるシミュレーション

  1. idehara says:

    おっと、「9」のカードが存在しない世界でシミュレーションしている。間違い探しは、学生の課題としよう。 🙂

    ヒント:かなり最初の方

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.