Python 套牌洗牌算法不是“;“人类”;足够地
为了好玩,我制作了这个牌堆洗牌功能来模拟人们如何不完美地洗牌牌。他们几乎把它切成两半,用“皱褶”的方法将左右甲板交织在一起,然后重复这个过程任意次数。甲板从未完美地编织在一起。您可以像L1、L2、R1、L3、R2、L4、R3等那样洗牌,或者像R1、L1、R2、R3、L2等那样洗牌。我的代码中的问题是,连续的重新洗牌永远不会切换牌组的第一张牌和最后一张牌。如果在第一次洗牌中,偏差表示将L1放在牌组的顶部,那么每次洗牌也会将L1放在牌组的顶部。我将Python 套牌洗牌算法不是“;“人类”;足够地,python,sorting,random,shuffle,Python,Sorting,Random,Shuffle,为了好玩,我制作了这个牌堆洗牌功能来模拟人们如何不完美地洗牌牌。他们几乎把它切成两半,用“皱褶”的方法将左右甲板交织在一起,然后重复这个过程任意次数。甲板从未完美地编织在一起。您可以像L1、L2、R1、L3、R2、L4、R3等那样洗牌,或者像R1、L1、R2、R3、L2等那样洗牌。我的代码中的问题是,连续的重新洗牌永远不会切换牌组的第一张牌和最后一张牌。如果在第一次洗牌中,偏差表示将L1放在牌组的顶部,那么每次洗牌也会将L1放在牌组的顶部。我将bias=random.random()移动到whi
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)
正如您所看到的,它总是将L1和Ln-1或R1和Rn-1作为输出组的第一个和最后一个数字,这取决于第一次洗牌的结果。不管我做了多少次改组。我做错了什么 我无法正确运行您的代码,因为您使用了一个名为“shuffle\u perfect”的函数,但您的代码似乎运行得很好。第一个和最后一个元素偶尔切换一次。但为了更好地进行调查,请在1000次迭代中查看列表中每个元素的直方图,并查看直方图的std。在这种情况下,您可以对问题做出更清楚的判断。我无法正确运行您的代码,因为您使用了一个名为“shuffle\u perfect”的函数,但您的代码似乎工作正常。第一个和最后一个元素偶尔切换一次。但为了更好地进行调查,请在1000次迭代中查看列表中每个元素的直方图,并查看直方图的std。在这种情况下,您可以对问题做出更清晰的判断。首先,我修改并添加到您的代码中,以使递归工作:
from random import random, randint
def shuffle_human(cards, reshuffles=0):
# creates a bias to imperfectly split deck +- 5% of the deck size
split = randint(-1*int(len(cards)/20), int(len(cards)/20))
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() # creates a bias to "incorrectly" choose
if L and bias <= .5: # which deck the next card will come from
# pops the card from the deck and appends it in.
l = L.pop(0)
# 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)
D.append(r)
# print(D)
if reshuffles > 0: # see if there are any reshuffles attempts needed
# recursive call to reshuffle the deck.
return shuffle_human(D, reshuffles-1)
return D
if __name__ == '__main__':
yes = 0
no = 0
deck = list(range(10))
for i in range(100000):
res = shuffle_human(deck, 3)
print(f'shuffle_human({deck}, 3):', res)
if res[0] == deck[0] and res[-1] == deck[-1]:
yes += 1
else:
no += 1
total = yes + no
print('\nunmixed:')
print(f'yes: {yes/total*100:.2f}%')
print(f'no: {no/total*100:.2f}%')
from random import random,randint
def shuffle_human(牌,洗牌=0):
#创建一个不完全分割甲板的偏差+-5%甲板尺寸
拆分=randint(-1*int(len(卡片)/20),int(len(卡片)/20))
L=卡片[:int(len(卡片)/2)+拆分]#创建左卡片组
R=牌[int(len(cards)/2)+拆分:]#创建右牌组
D=[]#清空新甲板
而len(D)0:#查看是否需要任何改组尝试
#递归调用以重新洗牌牌组。
返回洗牌(D,洗牌-1)
返回D
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
是=0
否=0
甲板=列表(范围(10))
对于范围内的i(100000):
res=人类洗牌(牌组,3)
印刷品(洗牌人({deck},3):',res)
如果res[0]==组[0]和res[-1]==组[-1]:
是+=1
其他:
否+=1
总计=是+否
打印(“\n混合:”)
打印(f'yes:{yes/total*100:.2f}%')
打印(f'no:{no/total*100:.2f}%')
运行此命令时,它会重复多次洗牌并进行计数,这似乎表明您的函数工作正常。可能问题是由您的shuffle\u perfect
功能引起的,在这种情况下,我们无法帮助您,因为您尚未共享该功能
仅供参考:当使用3的重新洗牌计数时,第一个和最后一个元素似乎保留了大约2%的时间。首先,我修改并添加到代码中,以使递归工作:
from random import random, randint
def shuffle_human(cards, reshuffles=0):
# creates a bias to imperfectly split deck +- 5% of the deck size
split = randint(-1*int(len(cards)/20), int(len(cards)/20))
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() # creates a bias to "incorrectly" choose
if L and bias <= .5: # which deck the next card will come from
# pops the card from the deck and appends it in.
l = L.pop(0)
# 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)
D.append(r)
# print(D)
if reshuffles > 0: # see if there are any reshuffles attempts needed
# recursive call to reshuffle the deck.
return shuffle_human(D, reshuffles-1)
return D
if __name__ == '__main__':
yes = 0
no = 0
deck = list(range(10))
for i in range(100000):
res = shuffle_human(deck, 3)
print(f'shuffle_human({deck}, 3):', res)
if res[0] == deck[0] and res[-1] == deck[-1]:
yes += 1
else:
no += 1
total = yes + no
print('\nunmixed:')
print(f'yes: {yes/total*100:.2f}%')
print(f'no: {no/total*100:.2f}%')
from random import random,randint
def shuffle_human(牌,洗牌=0):
#创建一个不完全分割甲板的偏差+-5%甲板尺寸
拆分=randint(-1*int(len(卡片)/20),int(len(卡片)/20))
L=卡片[:int(len(卡片)/2)+拆分]#创建左卡片组
R=牌[int(len(cards)/2)+拆分:]#创建右牌组
D=[]#清空新甲板
而len(D)0:#查看是否需要任何改组尝试
#递归调用以重新洗牌牌组。
返回洗牌(D,洗牌-1)
返回D
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
是=0
否=0
甲板=列表(范围(10))
对于范围内的i(100000):
res=人类洗牌(牌组,3)
印刷品(洗牌人({deck},3):',res)
如果res[0]==组[0]和res[-1]==组[-1]:
是+=1
其他:
否+=1
总计=是+否
打印(“\n混合:”)
打印(f'yes:{yes/total*100:.2f}%')
打印(f'no:{no/total*100:.2f}%')
运行此命令时,它会重复多次洗牌并进行计数,这似乎表明您的函数工作正常。可能问题是由您的shuffle\u perfect
功能引起的,在这种情况下,我们无法帮助您,因为您尚未共享该功能
仅供参考:当使用3的重新洗牌计数时,它似乎将第一个和最后一个元素的时间保持在2%左右,不确定它是否相关,但您不是递归调用
shuffle\u human
;您正在调用其他函数shuffle\u perfect
。不过,不要在简单的迭代中使用递归。缩进已关闭。我假设这是编译的,所以你没有正确复制,或者你混合了制表符和空格。可能需要删除整个帖子。。。。我还做了一个完美的洗牌算法,但没意识到我在那里调用了它。我不相信你展示的代码是生成这4行输出的代码。首先,代码中有print()语句没有反映在输出中,其次在命名方面存在差异。这是可以理解的,如果你