洗牌的最佳算法是什么? [英] What is the best algorithm to shuffle cards?

查看:74
本文介绍了洗牌的最佳算法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定有限的N张卡片, 洗牌的最佳方法(算法)是什么,所以我将以最少的步骤获得最好的洗牌包,以获得最大的随机排列?

Given a finite set of N cards, what is the best way (algorithm) to shuffle the cards so I will have the best shuffled pack of cards with minimum steps to get maximum random permutations?

在最少的步骤中最好的解决方案是什么?

What is the best solution in minimum steps?

推荐答案

使用 Fisher耶茨算法.许多编程语言都使用此算法的变体来随机播放有限集的元素.这是Fisher Yates算法的伪代码(Richard Durstenfeld优化的版本):

Use Fisher Yates algorithm. Many programming languages use variant of this algorithm to shuffle elements of finite set. This is the pseudo code of Fisher Yates algorithm (optimised version by Richard Durstenfeld):

-- To shuffle an array a of n elements (indices 0..N-1):
for i from N−1 downto 1 do
     j ← random integer such that 0 ≤ j ≤ i
     exchange a[j] and a[i]

此算法可确保均匀分布.对于N卡,可能会有 N!个随机组合.在此,N!排列中的任何排列均可能返回.时间复杂度为O(N).

This algorithm ensures uniform distribution. For N cards, there are N! shuffled combinations possible. Here any of N! permutations is equally likely to be returned. Time complexity is O(N).

这篇关于洗牌的最佳算法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆