Re: random numbers

new topic     » goto parent     » topic index » view thread      » older message » newer message

c.k.lester wrote:
> Wouldn't that possibly take too long generating the last number? You have a
> 51 out of 52 chance of picking a number the already exists. Nevermind...
> here's a test:

Well no one mentioned that speed was an issue. I just tried to demonstrate a
simple algorithm, I wasn't after speed. However, if you want speed, try this
adjustment to your fastest routine ...

  function shuff4()
    sequence s, n
    integer r
    s = fittytwo
    n = repeat(0,52)
    for t=52 to 2 by -1 do
     r = rand(t) -- grab a random position
     n[t] = s[r] -- add it to our new sequence
     s[r..51] = s[r+1..52]-- remove from available numbers
    end for
    n[1] = s[1]
    return n
  end function

On my machine I get ...

  shuff: 20-22% improvement
 shuff2: 24-27% improvement
 shuff3: 25-30% improvement
 shuff4: 85-95% improvement

The shuff4() routine avoids unnecessary calculations, and memory allocations and
deallocations.

-- 
Derek Parnell
Melbourne, Australia
Skype name: derek.j.parnell

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu