Shuffle generation
Eric S. Raymond
eric at snark.thyrsus.com
Tue May 14 01:12:52 AEST 1991
In one of my programs, I have a requirement to generate random shuffles
of arbitrary size. I am using the obvious method, as follows:
----------------------------- CUT HERE ------------------------------------
#define TRANSPOSITIONS(n) (10 * n)
void shuffle(size, shuffle)
int size, *shuffle;
{
int i, j;
for (i = 0; i < size; i++)
shuffle[i] = i;
for (i = 0; i < TRANSPOSITIONS(size); i++)
{
int holder, k;
j = roll(size);
k = roll(size);
holder = shuffle[k];
shuffle[k] = shuffle[j];
shuffle[j] = holder;
}
}
----------------------------- CUT HERE ------------------------------------
Two questions:
1) Is there a known way of choosing a formula for TRANSPOSITIONS(n) to
guarantee random mixing in some a priori sense?
2) This is, obviously, a naive method. Is there a `smart' algorithm that
works better?
Replies by email, please.
--
Eric S. Raymond = eric at snark.thyrsus.com (mad mastermind of TMN-Netnews)
More information about the Comp.lang.c
mailing list