RE: Random function

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

Jiri Babor wrote:
> Aku,
> 
> Random shuffle was already discussed here several times. 
> The function below does the trick. jiri
> 
> function shuffle(sequence s)
>     object temp
>     integer j
> 
>     for i = length(s) to 2 by -1 do
>         j = rand(i)
>         temp = s[j]
>         s[j] = s[i]
>         s[i] = temp
>     end for
>     return s
> end function
> 
> 
> ----- Original Message ----- 
> From: <aku at inbox.as>
> To: "EUforum" <EUforum at topica.com>
> Sent: Friday, March 09, 2001 1:33 AM
> Subject: Random function
> 
> 
> > How I can make function that accept sequence as input
> > for example {1,2,3,4,5,6,7,7.5,10,50,100} and output
> > that sequence but in random order such as
> > {5,100,4,7.5,7,1,2,50,10,3,6} as efficient as possible?
> > 
> > Thanks!
> > > function shuffle(sequence s)
>     object temp
>     integer j
> 
>     for i = length(s) to 2 by -1 do
>         j = rand(i)
>         temp = s[j]
>         s[j] = s[i]
>         s[i] = temp
>     end for
>     return s
> end function


The best shuffle i've run into so far looks like this:
 
 function shuffle(sequence s)
     object temp
     integer len,j

     len=length(s)
 
     for i = 1 to len do
         j = rand(len)
         temp = s[j]
         s[j] = s[i]
         s[i] = temp
     end for
     return s
 end function
--Noting that in an actual deck of cards, it is entirely possible
--that one or more cards get placed back in their original locations
--after a number of shuffles.


--Al

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

Search



Quick Links

User menu

Not signed in.

Misc Menu