简体   繁体   English

Fisher-Yates 算法解释?

[英]Fisher-Yates Algorithm Explanation?

I'm wondering if some of you understand how the Fisher-Yates shuffle works and can explain it to me.我想知道你们中的一些人是否了解 Fisher-Yates shuffle 的工作原理并且可以向我解释。 so I found this Fisher-Yates Shuffle code online:所以我在网上找到了这个 Fisher-Yates Shuffle 代码:

public function Main() {
var tempArray:Array = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
ShuffleArray(tempArray);
trace(tempArray);
}
public function ShuffleArray(input:Array)
{
for (var i:int = input.length-1; i >=0; i--)
{
var randomIndex:int = Math.floor(Math.random()*(i+1));
var itemAtIndex:Object = input[randomIndex];
input[randomIndex] = input[i];
input[i] = itemAtIndex;
}
}

That code works perfectly but I'm still confused该代码完美运行,但我仍然感到困惑

  1. I changed the loop to "input.length" and it doesn't work well, I still got "0" values sometimes.我将循环更改为“input.length”,但效果不佳,有时我仍然得到“0”值。 I have no idea why should I use "input.length-1" instead of "input.length"我不知道为什么要使用“input.length-1”而不是“input.length”
  2. At the randomize section, why should I randomize the index from 0 to the value (i+1), why don't we just randomize it from 0 to (i) instead?在随机化部分,为什么我应该将索引从 0 随机化到值 (i+1),为什么我们不将它从 0 随机化到 (i) 呢?

If some of you understand it, can you please explain it to me?如果你们中的一些人理解它,你能解释一下吗? Thank you so much非常感谢

  1. Js's array index starts at 0, so array a with length n 's last element is a[n -1] . Js 的数组索引从 0 开始,因此长度为n的数组a的最后一个元素是a[n -1]

  2. Math.random return a value from 0 to 0.9999...., but not include 1(range at [0, 1) ), so Math.random()* (i + 1) , would have a value from 0 to i + 0.999999...... , but not i + 1 (range [0, i+1) ), and use Math.floor to cut the dot parts to get a Integer , so we get a number in range [0, i] . Math.random 返回一个从 0 到 0.9999.... 的值,但不包括 1(range at [0, 1) ),所以Math.random()* (i + 1)的值从0i + 0.999999...... ,但不是i + 1 (range [0, i+1) ),并使用Math.floor切割点部分得到一个Integer ,所以我们得到一个范围[0, i]

let me explain with an example with negation lets says the array size is 10.让我用一个否定的例子来解释让我们说数组大小是 10。

1)if we use index.length line 3 in the for loop will read 1) 如果我们在 for 循环中使用 index.length 第 3 行将读取

input[randomIndex] = input[i] i.e.
input[randomIndex] = input[10];

but since javascript has 0 based arrays ,it has values from index 0 to 9 .index 10 will be out of bounds .Hence we should shuffle from last element(index 9 only)但由于 javascript 有基于 0 的数组,它具有从索引 0 到 9 的值。索引 10 将超出范围。因此我们应该从最后一个元素(仅索引 9)开始洗牌

2)for your second question if we use i instead of i+1. 2)对于您的第二个问题,如果我们使用 i 而不是 i+1。 lets say you are in the 1st iteration of the loop for index 9(will hold true for other iterations also).假设您处于索引 9 的循环的第 1 次迭代中(也适用于其他迭代)。 Here i is 9 as seen above .we want 9th index to be shuffled from any one of the indices from 0 to 9 Math.random will return from 0 to .999 and Math.floor will lower bound it so in our case,maximum value will be .999 * (9+1) = 9.99 .Math.floor will lower bound it to 9.So range is [0,9]这里 i 是 9,如上所示。我们希望第 9 个索引从 0 到 9 的任何一个索引中洗牌 Math.random 将从 0 返回到 0.999 并且 Math.floor 将下限,因此在我们的情况下,最大值将是 .999 * (9+1) = 9.99 .Math.floor 将其下限为 9.So 范围是 [0,9]

incase we used i maximum possible value would be 8 ie, Range[0,8]如果我们使用 i 的最大可能值是 8,即 Range[0,8]

Hence we use i+1 since we want values from [0,9]因此我们使用 i+1 因为我们想要来自 [0,9] 的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM