Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: shuffle array



On Mon, Dec 2, 2019 at 4:21 PM Stephane Chazelas
<stephane.chazelas@xxxxxxxxx> wrote:
> shouldn't that be "RANDOM % $#l + 1"?

No, the original code is correct. There is a loop invariant that
l[1,i-1] is shuffled before each iteration. An iteration makes l[1,i]
shuffled.

It's fairly easy to prove that this algorithm will produce each array
permutation with equal probability (assuming that we have access to a
function that gives a random number from [1, N] with uniform
distribution.)

> Or use rand48() in zsh/mathfunc

Thanks, TIL.

Roman.



Messages sorted by: Reverse Date, Date, Thread, Author