Re: Random number generator without duplicates?
Re: Random number generator without duplicates?
- Subject: Re: Random number generator without duplicates?
- From: Chris Espinosa <email@hidden>
- Date: Tue, 17 Apr 2001 14:56:06 -0700
>
Isn't the integer random number generator supposed to follow the same
>
rules
>
as the real, i.e. generate every value once before repeating?
No, it's not, because that wouldn't be random. Randomness is "every
time I ask for a number from 1 to 10, I have P=0.1 chance of getting any
number from 1 to 10". If a random number generator were constructed as
you propose, if it generated, say, 5 on the first iteration, then on the
second iteration the probability of generating any number would be
P=0.1111111... except for 5, where it would be P=0. Then if it
generated the number 3, the probability on the third pass would be P=0
for 3 and 5 and P=0.2 for all others, until it got to the tenth cycle,
where it would be P=1 for a certain number and P=0 for all others.
That's not random.
If you want a guaranteed nonrepeating cycle, you have to shuffle (as
already mentioned). A shuffling algorithm looks like this:
to create_shuffled_list(listLength)
set orderedList to {}
repeat with n from 1 to listLength
set end of orderedList to n
end repeat
set shuffledList to {}
repeat listLength times
set chosenItem to 0
repeat until chosenItem is not 0
set chosenItem to some item of orderedList
end repeat
set end of shuffledList to chosenItem
set item chosenItem of orderedList to 0
end repeat
return shuffledList
end create_shuffled_list
Chris