From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.5 required=5.0 tests=BAYES_00, PP_MIME_FAKE_ASCII_TEXT,REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII X-Google-Thread: 103376,c7fc42d2c6a0eedc X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-04-07 00:32:59 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!newsfeed.vmunix.org!news.cs.univ-paris8.fr!u-psud.fr!crihan.fr!univ-rennes1.fr!news.u-bordeaux.fr!news.cict.fr!not-for-mail From: doucet@laas.fr (Jean-Etienne Doucet) Newsgroups: comp.lang.ada Subject: Re: Ada2005 random Date: 7 Apr 2003 07:26:28 GMT Organization: LAAS/CNRS, Toulouse, France Message-ID: References: <3E8D787D.A3999D46@0.0> Reply-To: doucet@laas.fr NNTP-Posting-Host: siddhartha.laas.fr X-Trace: news.cict.fr 1049700388 19641 140.93.0.7 (7 Apr 2003 07:26:28 GMT) X-Complaints-To: postmaster@cict.fr NNTP-Posting-Date: 7 Apr 2003 07:26:28 GMT Xref: archiver1.google.com comp.lang.ada:35959 Date: 2003-04-07T07:26:28+00:00 List-Id: 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)