Random Shuffles
来源:互联网 发布:运动社交软件 推荐 编辑:程序博客网 时间:2024/05/22 00:30
This is more difficult. We have a list (x1,..., xn) and we wantto shuffle it randomly.
The following, surprisingly simple, algorithm does the trick:
begin
for i = n downto 2 begin
k = irand(1,i)
swap over xi and xk
end
end
Why does this work? It seems that it is not doing enough `mixing up'to produce a genuinely random shuffle. We can only prove that it worksif we have some notion of what we mean by a `random shuffle'. Let mekeep things simple and say that a shuffling process is random if eachelement of the list can end up in any position in the list with equalprobability. That is to say, in a list of n items there are npossible places that any given element can eventually occupy. I wanteach of these places to be equally likely.
So we now have to calculate the probability that our algorithm putselement xi into position j, where 1i, jn. If you lookcarefully at the algorithm you will see that element xi end up inposition j if it is not chosen on the first n - j steps ofthe algorithm and is chosen on the step n - j + 1. Since we knowthat irand(n, m) is genuine we can use elementary methods tofind that the probability of xi ending up in position j is
- Random Shuffles
- Perfect Shuffles
- random
- Random();
- random
- Random
- Random
- random
- Random
- Random
- Random
- Random
- Random
- Random
- random
- random
- random
- Random
- powerpoint中如何使用母版
- Converting VS2010 Solution to VS2008,将vs2010工程转换成vs2008
- 1. 开始
- winform学习积累
- logic:present 和 logic:empty的用法
- Random Shuffles
- 授权ARM架构,微软晚了三年
- HDU 1800(水题!!! @@)
- 正则表达式
- 习题 ct9_1:进制转换☆
- 最近比较烦
- 闲聊Google集群
- vs2010 msdn帮助文档安装
- Google数据中心内部运行解密 1800台服务器组成集群