This is a follow-up question to the Why shuffling by picking random position in all array instead of a part is not correct. I understand if I pick random numbers from all the range for 4 numbers every time the distribution won't be uniform. That's why improved Fisher–Yates shuffle algorithm picks numbers from the smaller range:

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

For 4 numbers it produces the the following ranges:

[0, 1]
[0, 1, 2]
[0, 1, 2, 3]

My question is whether it's important that the range starts from the 0 and increments sequentially? Are the following ranges generate uniform distribution as well?

[2]           --- start from the middle
[1, 2]        --- add one number to the left
[1, 2, 3]     --- add one number to the right
[0, 1, 2, 3]


  • 1
    $\begingroup$ One way to get a handle on this kind of "why" question is to read and understand the proof of correctness for the Fisher-Yates shuffle. A reasonable way to check whether your alternative proposal is correct might be to try to prove it correct (following the same methods used for proof of correctness of the standard Fisher-Yates shuffle), and see whether you're able to do so. $\endgroup$ – D.W. May 26 '17 at 15:07
  • $\begingroup$ I'd love to spend time on this, but unfortunately I don't have any :(. I'm practicing algorithms and instead of simply remembering that I should be pick up numbers from expanding range, I decided to understand why. But it turns out a pretty complex topic and requires significant time investment $\endgroup$ – Max Koretskyi May 26 '17 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.