Re: random numbers
- Posted by Jason Gade <jaygade at ?ahoo.co?> Nov 22, 2007
- 563 views
c.k.lester wrote: > > Allen Ashby wrote: > > > > I am trying to generate 52 random numbers 1-52 without duplicating any > > numbers. > > it seems every time I try I get duplications. Does anyone have a > > solution for this. > > Start with a sequence {1..52} and pull them from that. > > s = {1,2,3,4,5,6,7,8,9,...52} > n = {} > for t=1 to 52 do > r = rand(length(s)) -- grab a random position > n &= s[r] -- add it to our new sequence > s = s[1..r-1] & s[r+1..$] -- remove from available numbers > end for This would have been my solution as well. Nice! I would only change it slightly:
s = {1,2,3,4,5,6,7,8,9,...52} n = repeat(0, 52) for t=1 to 52 do r = rand(length(s)) -- grab a random position n[t] = s[r] -- add it to our new sequence s = s[1..r-1] & s[r+1..$] -- remove from available numbers end for
-- A complex system that works is invariably found to have evolved from a simple system that works. --John Gall's 15th law of Systemantics. "Premature optimization is the root of all evil in programming." --C.A.R. Hoare j.