gpt4 book ai didi

python - 牌组洗牌算法还不够 "human"

转载 作者:太空宇宙 更新时间:2023-11-03 19:48:59 25 4
gpt4 key购买 nike

为了好玩,我制作了这个牌组洗牌功能来模仿人们如何不完美地洗牌。他们将其切成近两半,并使用“褶边”方法将左右甲板交织在一起,然后重复此过程任意多次。甲板永远不会完美地编织在一起。您可以将它们打乱,如 L1、L2、R1、L3、R2、L4、R3 等或 R1、L1、R2、R3、L2 等。我的代码中的问题是,连续的洗牌永远不会切换这副牌的第一张和最后一张牌。如果在第一次洗牌中,偏差表明将 L1 放在牌堆的顶部,那么每次洗牌时也会将 L1 放在顶部。我将 bias = random.random() 移动到 while 循环中,因此它每次都会重置,因此有时会更改顺序。这是伪随机和递归代码的一些奇怪实例不能很好地协同工作吗?

def shuffle_human(cards,reshuffle):
split = random.randint(-1*int(len(cards)/20),int(len(cards)/20)) #creates a bias to imperfectly split deck +- 5% of the deck size
L = cards[:int(len(cards)/2)+split] # creates left deck
R = cards[int(len(cards)/2)+split:] # creates right deck
D =[] # empty new deck
while len(D)< len(cards):
bias = random.random() # creates a bias to "incorrectly" choose
if L and bias <=.5: # which deck the next card will come from**strong text**
l = L.pop(0) # pops the card from the deck and appends it in.
print(l) # formatted this way so i can see whats going on
D.append(l)
if R and bias >.5: # same thing for right deck
r = R.pop(0)
print(r)
D.append(r)
print(D)
if reshuffle>0: # see if there are any reshuffles attempts needed
shuffle_perfect(D,reshuffle-1) # recursive call to reshuffle the deck.

shuffle_human(deck,3)

输出有问题

 [0, 5, 6, 7, 8, 1, 9, 10, 2, 3, 4]   # initial shuffle
[0, 1, 5, 9, 6, 10, 7, 2, 8, 3, 4] # reshuffle 1
[0, 10, 1, 7, 5, 2, 9, 8, 6, 3, 4] # 2
[0, 2, 10, 9, 1, 8, 7, 6, 5, 3, 4] # 3

正如您所看到的,输出牌组的第一个和最后一个数字始终是 L1 和 Ln-1 或 R1 和 Rn-1,具体取决于第一次洗牌的结果。无论我进行多少次洗牌。我究竟做错了什么?

最佳答案

我无法正确运行您的代码,因为您使用了名为“shuffle_perfect”的函数,但您的代码似乎工作得很好。第一个和最后一个元素偶尔会切换一次。但为了更好地研究,请查看列表中每个元素在 1000 次迭代中的直方图,并查看直方图的标准差。在这种情况下,您可以对问题做出更清晰的判断。

关于python - 牌组洗牌算法还不够 "human",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59956618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com