这个简单的混洗算法似乎会产生有偏差的结果:
# suppose $arr is filled with 1 to 52
for ($i < 0; $i < 52; $i++) {
$j = rand(0, 51);
# swap the items
$tmp = $arr[j];
$arr[j] = $arr[i];
$arr[i] = $tmp;
}
你可以试试..。不使用52,而是使用3(假设只使用3张牌),并运行10,000次并对结果进行汇总,您将看到结果偏向于某些模式……
问题是。什么是它将会发生的简单解释?
正确的解决方案是使用类似于
for ($i < 0; $i < 51; $i++) { # last card need not swap
$j = rand($i, 51); # don't touch the cards that already "settled"
# swap the items
$tmp = $arr[j];
$arr[j] = $arr[i];
$arr[i] = $tmp;
}
但问题是...为什么第一种方法,看起来也是完全随机的,会使结果有偏差?
更新1:感谢这里的人们指出它需要是兰特($i,51)才能正确地洗牌。
https://stackoverflow.com/questions/859253
复制相似问题