[104874] in Cypherpunks

home help back first fref pref prev next nref lref last post

Re: Random array

daemon@ATHENA.MIT.EDU (=?iso-8859-1?Q?Jes=FAs?= Cea =?iso)
Fri Oct 30 10:49:06 1998

Date: Fri, 30 Oct 1998 14:59:05 -0100
From: =?iso-8859-1?Q?Jes=FAs?= Cea =?iso-8859-1?Q?Avi=F3n?= <jcea@argo.es>
To: Lista Coderpunks <coderpunks@toad.com>
Reply-To: =?iso-8859-1?Q?Jes=FAs?= Cea =?iso-8859-1?Q?Avi=F3n?= <jcea@argo.es>

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


home help back first fref pref prev next nref lref last post