On Mon, 22 Dec 2014, Ted Unangst wrote:

> I would like to generate a permutation of some lines. We have random,
> which is vageuly similar. This adds a -p option to instead permute
> instead of randomly select.

> +     for (j = numlines; j > 1; j--) {
> +             size_t s = arc4random_uniform(j);
> +             char *tmp = array[s];
> +             array[s] = array[j - 1];
> +             array[j - 1] = tmp;
> +     }

You could use the "forward" Knuth shuffle like that used in ip_id.c to
perform the permutation while the lines are being read and avoid the
extra pass.

Some people have said that this is "less random" than the traditional
backwards shuffle, but none have ever provided an argument why and I
couldn't see how they differ myself.

-d

Reply via email to