Re: Random array

New Message Reply About this list Date view Thread view Subject view Author view

Anonymous (nobody@replay.com)
Thu, 29 Oct 1998 17:33:29 +0100


A couple responders have said there is no SWAP_TIMES
which would work. But I don't understand why the
following wouldn't work:

First of all, make sure that x and y can't be equal, since
there's no point in swapping an element with itself. This
should add a negligible amount of time.

        x=getrand();
        do
                y=getrand();
        while (x == y);

Now, simply calculate how many SWAP_TIMES it would take for it
to be equally as likely that an element would not be touched as
it would to land in its original spot.

(255/256)^(t*2) = 1/256

or

t = ln(1/256)/(2*ln(255/256))

or

708.3955

Another anonymous wrote:

In the following snippet of pseudo-code, what should the value of
SWAP_TIMES be to make the array A[] random, assuming
that getrand() returned a truly random integer between
0 and 255
                                                                                
A[256];
                                                                                
for(i=0;i<SWAP_TIMES;i++){
        x=getrand();
        y=getrand();
        swap(A[x],A[y]);
}
                                                                                
Thanks


New Message Reply About this list Date view Thread view Subject view Author view

 
All trademarks and copyrights are the property of their respective owners.

Other Directory Sites: SeekWonder | Directory Owners Forum

The following archive was created by hippie-mail 7.98617-22 on Sat Apr 10 1999 - 01:15:23