Re: Randomly outputting an array
- From: "toby" <toby@xxxxxxxxxxxxxxxxxxx>
- Date: 16 Jul 2006 11:34:46 -0700
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".
Thankyou, I'm glad somebody pointed this out.
It turns out, there are lots of ways of shuffling an array that
will not[1] really shuffle it properly. The key is getting every
element to have an equal chance of popping up in any other location.
That may be a little harder than it looks, or at least a little
easier to screw up than it looks.
Unfortunately, I only have a second to write this post, but I remember
taking a probability course in college and, while I was taking it,
looking into this exact issue. I looked at the Fisher-Yates algorithm
for shuffling an array and I realized why it's structured exactly the
way it is and why it has to be that way.
That is probably the simplest correct method, apparently the same as
Algorithm P given by Professor Knuth in Seminumerical Algorithms, 3.4.2
Random Sampling and Shuffling. I think a couple of other posters here
have suggested it. A lot of people call this the "Knuth shuffle", but
he credits L.E. Moses and R.V. Oakford (1963).
See: http://en.wikipedia.org/wiki/Knuth_shuffle
I wish I had more time to delve into this right now, and that my
knowledge of probability were still fresh, but I believe it is
important, if you care about having a truly random arrangement of
array elements afterwards (rather than having slightly higher odds
of elements at the beginning ending up at the end or something)
to use the right algorithm.
- Logan
[1] even assuming a random number generator that generates truly
random numbers.
.
- 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
- Randomly outputting an array
- Prev by Date: Re: Randomly outputting an array
- Next by Date: Dynamically Creating Variables in C#
- Previous by thread: Re: Randomly outputting an array
- Next by thread: Re: Randomly outputting an array
- Index(es):
Relevant Pages
|