comp.lang.ada
 help / color / mirror / Atom feed
From: doucet@laas.fr (Jean-Etienne Doucet)
Subject: Re: Ada2005 random
Date: 7 Apr 2003 07:26:28 GMT
Date: 2003-04-07T07:26:28+00:00	[thread overview]
Message-ID: <b6r974$j5p$1@news.cict.fr> (raw)
In-Reply-To: 3E8D787D.A3999D46@0.0

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1555 bytes --]

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

For this kind of problems, I use the Ada.Numerics.Float_Random generator,
along with the function:

  function Rand (N : Positive) return Positive is
     -- gives a random integer in the range 1 .. N
  begin
     return Integer(Float(N) * Random(Gen) + 0.5);
  end Rand;


It can be used for ranges other than 1 .. N:

  function Rand (Min, Max : Integer) return Integer is
     -- gives a random integer in the range Min .. Max
  begin
     return Rand(Max - Min + 1) + Min - 1;
  end Rand;


It works as well for enum types:

  type Enum is (...);
  
  function Random_Enum return Enum is 
     -- gives a random Enum value
  begin
     return Enum'Val(Rand(Enum'Pos(Enum'Last) + 1) - 1);
  end Random_Enum;


I've never had a problem with this.

_______________________________________________________________________________
Jean-Etienne Doucet / LAAS-CNRS / Toulouse, France       E-mail: doucet@laas.fr
"S'il n'y a pas de solution, c'est qu'il n'y a pas de probl�me."  (Les Shadoks)






  reply	other threads:[~2003-04-07  7:26 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
2003-04-04 12:20     ` Stuart Palin
2003-04-07  7:26       ` Jean-Etienne Doucet [this message]
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