Shuffle generation
Dik T. Winter
dik at cwi.nl
Thu May 16 22:12:53 AEST 1991
In article <1991May15.183447.1437 at proto.com> joe at proto.com (Joe Huffman) writes:
> >To swap N items in an array A[0..(n-1)], the following O(N) algorithm works:
>
> >for (i=0; i < N; i++) {
> > swap(i, i + random(N - i));
> >}
>
> I used a similar algorithm at first (swap(random(N),random(N))
> and discovered that it wasn't random enough.
This is not at all similar.
>
> void shuffle(int n, list new_list, list org_list)
> {
> while(n)
> {
> element temp = get_and_remove_element(org_list,random(n));
> add_element(new_list,temp);
> n--;
> }
> }
And if you look long enough you will see that this is equivalent to the
originally proposed algorithm.
--
dik t. winter, cwi, amsterdam, nederland
dik at cwi.nl
More information about the Comp.lang.c
mailing list