Generate an uniform random permutation

前端 未结 1 1546
没有蜡笔的小新
没有蜡笔的小新 2020-12-30 10:37

I am not sure whether the following pseudo-code can generate an uniformly random permutation:

PERMUTATE(A): 
    n = A.length
    for i = 1 to n         


        
相关标签:
1条回答
  • 2020-12-30 10:59

    This solution is biased, you want the Fisher Yates algorithm [which is similar] for non biased permutation. [basically, you need to swap with random(i,n) instead of with random(1,n)]

    This thread discusses how and why your solution is biased.

    0 讨论(0)
提交回复
热议问题