Re: Random number generator without duplicates?
Re: Random number generator without duplicates?
- Subject: Re: Random number generator without duplicates?
- From: Paul Berkowitz <email@hidden>
- Date: Tue, 17 Apr 2001 19:43:11 -0700
On 4/17/01 6:27 PM, "Michelle Steiner" <email@hidden> wrote:
>
On 4/17/01 2:56 PM, Chris Espinosa <email@hidden> wrote:
>
>
> If you want a guaranteed nonrepeating cycle, you have to shuffle (as
>
> already mentioned). A shuffling algorithm looks like this:
>
>
That's very nice; it's going into my library. To get six random numbers
>
out of fifty six, you'd call the handler with something like this, right?
>
>
items 1 through 6 of create_shuffled_list(56) It really doesn't matter
>
which six items are used because they're in random order, so the first
>
six are as good as any.
>
If that's the case (and I'm not yet 100% certain that it is) then you don't
have to get all 56 items shuffled. Just stop when you hit no. 6 :
to create_shuffled_sublist(totalNumberSet, listLength)
set orderedList to {}
repeat with n from 1 to totalNumberSet
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_sublist
create_shuffled_sublist(56, 6)
-- {38, 13, 32, 52, 40, 8}