Re: Random array

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

Jesús Cea Avión (jcea@argo.es)
Fri, 30 Oct 1998 14:59:05 -0100


> >for j from 255 down to 1
> > pick random number k from 0 through j
> > swap items j and k

I'd rather prefer

for i in 1..n
  j = random(1..n)
  swap(a[i],a[j])

So, each position has equal posibilities. Since random+random=random
:-), each cell is visited one time, and the element placed there is
random and taken from the entire set, the final array will be random.

-- 
Jesus Cea Avion                         _/_/      _/_/_/        _/_/_/
jcea@argo.es http://www.argo.es/~jcea/ _/_/    _/_/  _/_/    _/_/  _/_/
                                      _/_/    _/_/          _/_/_/_/_/
PGP Key Available at KeyServ   _/_/  _/_/    _/_/          _/_/  _/_/
"Things are not so easy"      _/_/  _/_/    _/_/  _/_/    _/_/  _/_/
"My name is Dump, Core Dump"   _/_/_/        _/_/_/      _/_/  _/_/
"El amor es poner tu felicidad en la felicidad de otro" - Leibnitz


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