Why is fishing the most useful shuffling algorithm?

Could you say that the most up-to-date version of the fishing nursery is the most unbiased shuffling algorithm? How would you explain that each element in an array has a probability that 1 / n is in its original location?

+3
source share
2 answers

Given the ideal pseudo-random number generator ( Mersenne Twister is very close), the Fisher-Yates algorithm is completely unbiased in that each permutation has an equal probability of occurrence. This is easy to prove using induction. The Fisher-Jace algorithm can be written recursively as follows (in the pseudocode of Python syntax):

def fisherYatesShuffle(array):
    if len(array) < 2:
        return

    firstElementIndex = uniform(0, len(array))
    swap(array[0], array[firstElementIndex])
    fisherYatesShuffle(array[1:])

firstElementIndex. , .

: . , , , . , , ( , ) , . Pierson Chi-square Test .

+7

(Modern, aka "Knuth" ) Fisher-Yates shuffle -

  • O (n) O (1) O (0)
  • unbiased ( )
  • / , , .

( , , - , )?

Edit: " , , . ( , ...)
, , RNG, .
, m, , n, 1, , n , , . , , ( ). , , , , 1/, . .

+3

Source: https://habr.com/ru/post/1737256/


All Articles