我的朋友建议我在面试之前尝试解决这个问题,但我不知道如何处理它。 我需要编写一个代码,在不使用内置标准随机函数的情况下洗牌一副52张牌。
更新 感谢吴逸飞,他的回答非常有帮助。 这是我的github项目的链接,我执行了给定的算法 https://github.com/Dantsj16/Shuffle-Without-Random.git
答案 0 :(得分:0)
您的问题并不是说它必须是52张卡的随机随机播放。有一个像完美洗牌这样的东西,其中一个浅滩洗牌完成顶部卡保持在顶部,每个其他卡来自甲板的另一半。许多魔术师和卡鲨可以根据需要进行洗牌。众所周知,如果顶部卡片在每次洗牌时保持在最顶层,那么标准52张牌组中的八个完美洗牌会将卡片恢复到原始顺序。
以下是python中的8个完美shuffle注意,这个shuffle的执行方式与实际的手动shuffle不同,以简化代码。
In [1]: d0=[x for x in range(1,53)] # the card deck
In [2]: print(d0)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52]
In [3]: d1=d0[::2]+d0[1::2] # a perfect shuffle
In [4]: print(d1)
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52]
In [5]: d2=d1[::2]+d1[1::2]
In [6]: d3=d2[::2]+d2[1::2]
In [7]: d4=d3[::2]+d3[1::2]
In [8]: d5=d4[::2]+d4[1::2]
In [9]: d6=d5[::2]+d5[1::2]
In [10]: d7=d6[::2]+d6[1::2]
In [11]: d8=d7[::2]+d7[1::2]
In [12]: print(d8)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52]
In [13]: print(d0 == d8)
True
如果你想手动完成洗牌,请使用
d1=[None]*52
d1[::2]=d0[:26]
d1[1::2]=d0[26:]
这为d1
提供了
如果您确实需要随机播放,请告诉我。如果你需要,我可以将我的Borland Delphi代码改编成python。