1

我创建了一个类,可以有效地从 52 张牌组中随机发一张扑克牌。然后我写了几行代码来模拟 52 次绘制的 100K 模拟,因为我想知道分布是否正确实现。当我这样做时,我意识到运行 sim 需要 87 秒。这对我来说似乎很长一段时间。任何人都可以指出#2 中的一些可能使它变得如此缓慢的事情吗?

import time
import random as rand
import numpy as np
class PlayingCard:
    ranks = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
    suits = ['Spades', 'Hearts', 'Clubs', 'Diamonds']
    def __init__(self, rank = None, suit = None):
        if rank is None: self.rank = PlayingCard.ranks[rand.randint(0,12)]
        elif rank in PlayingCard.ranks: self.rank = rank
        else: raise NameError('Invalid rank')

        if suit is None: self.suit = PlayingCard.suits[rand.randint(0,3)]
        elif suit in PlayingCard.suits: self.suit = suit
        else: raise NameError('Invalid suit')

    def identity(self):
        return (self.rank,self.suit)
#2
start = time.clock()

deck = zip(PlayingCard.ranks*4,PlayingCard.suits*13)
mat = [[PlayingCard().identity() for x in range(52)] for y in range(100000)]
res = [[(y.count(x)/52.0) for x in deck] for y in mat]
mean = [np.mean([res[y][x] for y in range(len(res))]) for x in range(52)]

end = time.clock() - start
print end
4

1 回答 1

4

为什么不只计算抽奖次数?它更快:

import pprint
import random

from collections import defaultdict

ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
suits = ['Spades', 'Hearts', 'Clubs', 'Diamonds']

deck = zip(ranks * 4, suits * 13)

def test(trials=100000):
    draws = defaultdict(int)

    for i in range(trials):
        draws[random.choice(deck)] += 1

    return {card: float(value) / trials for card, value in draws.iteritems()}


pprint.pprint(test(100000))

这对我来说大约需要0.6几秒钟:

{('10', 'Clubs'): 0.01961,
 ('10', 'Diamonds'): 0.01897,
 ('10', 'Hearts'): 0.0196,
 ('10', 'Spades'): 0.01902,
 ('2', 'Clubs'): 0.01953,
 ('2', 'Diamonds'): 0.0201,
 ('2', 'Hearts'): 0.01889,
 ('2', 'Spades'): 0.01891,
 ('3', 'Clubs'): 0.01943,
 ('3', 'Diamonds'): 0.0198,
 ('3', 'Hearts'): 0.01893,
 ('3', 'Spades'): 0.01953,
 ('4', 'Clubs'): 0.01973,
 ('4', 'Diamonds'): 0.01946,
 ('4', 'Hearts'): 0.01822,
 ('4', 'Spades'): 0.01931,
 ('5', 'Clubs'): 0.01845,
 ('5', 'Diamonds'): 0.01956,
 ('5', 'Hearts'): 0.01978,
 ('5', 'Spades'): 0.01943,
 ('6', 'Clubs'): 0.01852,
 ('6', 'Diamonds'): 0.01903,
 ('6', 'Hearts'): 0.01928,
 ('6', 'Spades'): 0.01848,
 ('7', 'Clubs'): 0.0195,
 ('7', 'Diamonds'): 0.01881,
 ('7', 'Hearts'): 0.0194,
 ('7', 'Spades'): 0.01926,
 ('8', 'Clubs'): 0.01946,
 ('8', 'Diamonds'): 0.0188,
 ('8', 'Hearts'): 0.01985,
 ('8', 'Spades'): 0.01875,
 ('9', 'Clubs'): 0.01914,
 ('9', 'Diamonds'): 0.01908,
 ('9', 'Hearts'): 0.01937,
 ('9', 'Spades'): 0.01838,
 ('A', 'Clubs'): 0.01935,
 ('A', 'Diamonds'): 0.01843,
 ('A', 'Hearts'): 0.01957,
 ('A', 'Spades'): 0.01852,
 ('J', 'Clubs'): 0.01992,
 ('J', 'Diamonds'): 0.01933,
 ('J', 'Hearts'): 0.01881,
 ('J', 'Spades'): 0.01946,
 ('K', 'Clubs'): 0.01932,
 ('K', 'Diamonds'): 0.01845,
 ('K', 'Hearts'): 0.01935,
 ('K', 'Spades'): 0.02015,
 ('Q', 'Clubs'): 0.0189,
 ('Q', 'Diamonds'): 0.01942,
 ('Q', 'Hearts'): 0.01977,
 ('Q', 'Spades'): 0.01988}
于 2012-12-27T01:52:17.427 回答