Re: random numbers

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

jiri babor wrote:
> 
> sequence s
> 
> function shuffle(sequence s)    -- random shuffle of sequence
>     object temp
>     integer r
>     for i = length(s) to 2 by -1 do
>         r = rand(i)
>         temp = s[r]
>         s[r] = s[i]
>         s[i] = temp
>     end for
>     return s
> end function
> 
> s =
> {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52}
> s = shuffle(s)

Good to see you, jiri!

Derek Parnell wrote:
> 
> 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

I never thought that speed was an issue myself -- I just thought that it was a
simpler or more elegant way of doing things.

Yes, jiri's is best though.

jiri babor wrote:
> 
> Yes, Derek, a built-in swap function would be more useful than endless
> discussions
> how to simulate a simple goto with a dozen fancy constructs...
> 
> jiri

Well, while I'm one of the "anti-gotos" and prefer a few clear constructs
instead, I think that there are quite a few interesting sequence operations that
I would like to see built-in to the interpreter.

Then again, we always discuss them and never come to any kind of consensus.

Does any other compiled or interpreted language have a built-in swap? I can't
recall coming across one, even though it is a pretty common operation.

--
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.

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

Search



Quick Links

User menu

Not signed in.

Misc Menu