Re: Randomly outputting an array
- From: Logan Shaw <lshaw-usenet@xxxxxxxxxxxxx>
- Date: Tue, 18 Jul 2006 00:46:09 GMT
Arthur J. O'Dwyer wrote:
On Sun, 16 Jul 2006, Logan Shaw wrote:Ico wrote:Ico <usenet@xxxxxxx> wrote:
Is N times swapping two elements considered enough to truly 'randomize'
an array ?
That was a stupid question, please ignore me...
Actually, it's not a stupid question. It's a very good question.
The answer is definitely "no".
No, the answer is "it depends."
My knee-jerk reaction was also "no", but then I realized that Ico was
replying to Richard Heathfield's post, which is an obvious "yes" (or
should be, if he didn't make any unnecessary mistakes).
I guess it depends on whether the question is
1. "Is it really possible that there are correct algorithms for
randomizing an array that can accomplish it with as little
as N swaps of two elements?"
or
2. "Is it true that merely swapping two elements N times will
guarantee correct randomization of the array?"
I was interpreting the question as #2, but it might've been meant
as #1.
Picking N pairs uniformly at random and swapping them will definitely
not shuffle the array, since there are (N^2)^N = N^(2N) ways to pick
N pairs, and N! ways to shuffle the array, and N^(2N) is generally not
a multiple of N!.
Yeah, but more than that, even visiting every single element of the
array isn't necessarily correct. Here's an algorithm that looks at
first like it might be good for shuffling an array:
int a[N] = { ... some stuff ... };
for (int i = 0; i < N; i++)
{
j = random_in_range_inclusive (i+1, N-1);
swap (& a[i], & a[j]);
}
At first glance, that algorithm has some good characteristics. It
visits every element in the array, so there is no way it will leave
an element untouched accidentally. It puts every element into a
random position, so everything gets moved. But it's not correct.
You can show that it's incorrect quite easily: it will always take
the first element in the array and put it somewhere other than the
first position. But here's the kicker for this particular algorithm:
any algorithm which correctly randomly shuffles an array of length N
will have a probability of 1/(N!) of giving you back exactly the same
ordering you started with. After all, the ordering you started with
is one of the N! possible orderings, so it should be represented with
a probability equal to all the other orderings. But the above code
always moves the first element somewhere else, so it gives you a zero
probability of giving you back your original order; therefore, it's
not a correct algorithm.
Anyway, the point was just that there are lots of ways for this to
go wrong if you don't understand what you're really after, namely
choosing one of the N! possible orderings at random and with equal
probability as any of the others.
- Logan
.
- References:
- Randomly outputting an array
- From: ryanarossi@xxxxxxxxx
- Re: Randomly outputting an array
- From: Richard Heathfield
- Re: Randomly outputting an array
- From: Ico
- Re: Randomly outputting an array
- From: Ico
- Re: Randomly outputting an array
- From: Logan Shaw
- Re: Randomly outputting an array
- From: Arthur J. O'Dwyer
- Randomly outputting an array
- Prev by Date: Re: Choice of languages / environments
- Previous by thread: Re: Randomly outputting an array
- Next by thread: Re: Randomly outputting an array
- Index(es):
Relevant Pages
|