我想为3X5老虎机生成顺序结果。 我有5个不同长度的卷轴,例如:
reel1 = [1,2,3,4,5],
reel2 = [2,3,4,5,6,7],
reel3 = [3,4,5,6,7,8,9],
reel4 = [4,5,6,7,8,9,0,1],
reel5 = [0,1,2].
现在我正在使用Python for循环来生成结果,但我认为它可能不是一种有效的方式,因为我总共需要5个循环,如果卷轴长度很长,则需要很长时间时间顺序生成。
我认为使用Python可能会有更有效的方法。
任何人都有任何想法?〜
答案 0 :(得分:4)
如果您想要随机结果,可以使用random.choice
:
from random import choice
reels = [reel1, reel2, ...]
outcome = [choice(reel) for reel in reels]
如果您想要所有结果,请使用itertools.product
:
from itertools import product
for outcome in product(*reels):
# use outcome
如果你澄清了你需要三个数字的集合,我会预先生成位置:
reelpos = []
for reel in reels:
reelpos.append(list(zip(reel,
reel[1:] + reel[:1],
reel[2:] + reel[:2])))
然后,您可以将choice
或product
应用于reelpos
,如下所示:
[[(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 1), (5, 1, 2)],
[(2, 3, 4), (3, 4, 5), (4, 5, 6), (5, 6, 7), (6, 7, 2), (7, 2, 3)],
[(3, 4, 5), (4, 5, 6), (5, 6, 7), (6, 7, 8), (7, 8, 9), (8, 9, 3),
(9, 3, 4)],
[(4, 5, 6), (5, 6, 7), (6, 7, 8), (7, 8, 9), (8, 9, 0), (9, 0, 1),
(0, 1, 4), (1, 4, 5)],
[(0, 1, 2), (1, 2, 0), (2, 0, 1)]]
答案 1 :(得分:0)
不确定您所谓的“生成顺序结果”。如果您想到的是所有可能结果的枚举,请继续使用五个循环:将有5 x 6 x 7 x 8 x 3 = 5040种不同的组合,而不是这么大的数字。
使用Python(3200000组合),即使是5个20位数的转轴仍然可以管理。