comp.lang.ada
 help / color / mirror / Atom feed
From: Mark Biggar <mark.a.biggar@attbi.com>
Subject: Re: Ada2005 random
Date: Sun, 13 Apr 2003 21:58:38 GMT
Date: 2003-04-13T21:58:38+00:00	[thread overview]
Message-ID: <i4lma.119783$ug3.236943@rwcrnsc51.ops.asp.att.net> (raw)
In-Reply-To: <3E997A99.3060506@attbi.com>

Robert I. Eachus wrote:
> Dale Stanbrough wrote:
> 
>> Randy Brukardt wrote:
>>
>>
>>> I needed that for a 'choosing' algorithm, say drawing balls one by one
>>> out of a bowl. If you have 8 balls originally, you're choosing 1 out of
>>> 7, then 1 out of 6, etc. If you try to use 1 out of 8 the whole time and
>>> discarding the useless ones, the second last draw can take a very long
>>> time (only 2 out of 8 choices are meaningful). I'm pretty sure that this
>>> is a valid technique; certainly the underlying float generator is still
>>> random (at least as much as it ever was!), and the use of that result is
>>> unbiased.
>>>
>>> While it would be nice if Ada supported this directly, it's easy enough
>>> to write it yourself.
> 
> 
> If you need a permutation, the best way to do it is to generate a set of 
> random (float) values, associate them with the data to be permuted and 
> the sort on this key.  I have seen a lot of code that attempts to 
> permute a list by going through the list and exchanging elements.  For 
> lists of length greater than two this can easily be shown to be biased.
> 
> For example there are 3! = 6 possible permutions of a list of three 
> elements, the usual version of the swapping algorithm has 3^3 = 27 
> possible outcomes, another (worse) variation has 2^3 = 8 different 
> outcomes.  In either case the number of outcomes is not evenly divisible 
> by the number of different permutations.

The way I usualy do random array shuffle has no bias

for i in reverse 2..n loop
	j := random(1..i);
	swap(x(i), x(j));
end loop;

that translates to "randomly select with out replacement until the
unselected set is empty"  and in the 3 item case results in exactly
6 outcomes (first iteration select 1 of 3, second iteration select
1 of 2 for 6 outcomes).

-- 
Mark Biggar
mark@biggar.org
mark.a.biggar@attbi.com




  reply	other threads:[~2003-04-13 21:58 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-04-03 12:27 Ada2005 random David C. Hoos, Sr.
2003-04-03 12:39 ` Peter Hermann
2003-04-03 22:10   ` Randy Brukardt
2003-04-04  7:43     ` Peter Hermann
2003-04-04 10:21     ` Dale Stanbrough
2003-04-04 12:11       ` Stuart Palin
2003-04-04 14:25       ` Lutz Donnerhacke
2003-04-04 21:51         ` Dale Stanbrough
2003-04-05 18:34           ` Samuel Tardieu
2003-04-05  6:46       ` Martin Krischik
2003-04-07 21:07         ` Randy Brukardt
2003-04-13 14:56       ` Robert I. Eachus
2003-04-13 21:58         ` Mark Biggar [this message]
2003-04-04 12:20     ` Stuart Palin
2003-04-07  7:26       ` Jean-Etienne Doucet
2003-04-07  8:09         ` Lutz Donnerhacke
2003-04-04  8:27   ` 
2003-04-04 12:14     ` Peter Hermann
2003-04-04 14:26       ` Lutz Donnerhacke
     [not found]     ` <6lk1m-lm3.ln1@beastie.ix.netcom.com>
2003-04-05  7:29       ` Pascal Obry
2003-04-05  9:00         ` tmoran
2003-04-05 17:02           ` Pascal Obry
  -- strict thread matches above, loose matches on Subject: below --
2003-04-02 13:27 Peter Hermann
2003-04-02 13:44 ` Lutz Donnerhacke
2003-04-03  9:56   ` Peter Hermann
2003-04-03 10:13     ` Lutz Donnerhacke
2003-04-04  3:50 ` Steve
2003-04-04 14:30   ` Lutz Donnerhacke
2003-04-05  3:02     ` Steve
2003-04-04  4:33 ` Christoph Grein
2003-04-06 15:44   ` 
2003-04-05  0:14 ` Jeffrey Carter
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox