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=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,5291fa0ea59b8b29 X-Google-Attributes: gid103376,public From: jp3@rl.ac.uk ( Dr J Parker) Subject: Re: Random Number Generation Date: 1996/09/30 Message-ID: <52p14c$h7e@newton.cc.rl.ac.uk>#1/1 X-Deja-AN: 186309604 references: <5266q1$a0t@netty.york.ac.uk> organization: Rutherford Appleton Laboratory, Oxon, UK newsgroups: comp.lang.ada Date: 1996-09-30T00:00:00+00:00 List-Id: Tucker Taft suggests: "In any case, Ada.Numerics.Float_Random is the easiest one to adapt to your use, since it always generates values between 0.0 and 1.0. You can then multiply that value by an appropriate integer or float and add the low bound to produce the desired dynamic range. declare Result : Integer range Low_Bound .. High_Bound; Range_Length : constant Integer := High_Bound - Low_Bound + 1; begin Result := (Integer(Float(Range_Length) * Ada.Numerics.Float_Random.Random(Gen)) mod Range_Length) + Low_Bound; ... " Careful, there are some limits in which this fails. Simple example: Suppose you have a high quality random number generator R1 in the range 0 .. 127 and you want to turn it into a high quality random number generator R2 in the range 0 .. 82. Only the rejection method works: you must throw out at least 45 of the values returned by R1. eg, if R1 returns a number in the range 83..127, then reject it and keep calling R1 until you get a number in the range 0 .. 82. You'll get a stream of high quality random numbers in the range 0 .. 82. In general, you reject just enough to make the number of elements (think bins) in range of R1 an integer multiple of the number of elements in the range of R2 (you reject never more than 50% of the number of elements in range of R1). Then you can use "*"'s, etc to transform R1 into R2. Floating point doesn't change the problem.