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,6bc361dc425f4265 X-Google-Attributes: gid103376,public From: Geert Bosch Subject: Re: Random Number problems Date: 1997/10/01 Message-ID: <60tprq$lci$1@gonzo.sun3.iaf.nl>#1/1 X-Deja-AN: 277090428 References: <60q1hm$8oq$1@goanna.cs.rmit.edu.au> <3431B598.41C6@activist.com> Organization: La Calandre Infortunee Newsgroups: comp.lang.ada Date: 1997-10-01T00:00:00+00:00 List-Id: David Muhammad wrote: >I am trying to write a function that will use a Random Number Generator and then return a random value between 1 and 52. I have written the Random Number Generator function but how do I tell the compiler to select a number between 1 and 52. a) declare a subtype subtype pseudorand is range 1..52 b) Call my randomnumber from function Rand_Num c) NOw somehow convert some "Number" to a range between 1 and 52. I am stuck ?? A quick quote from the nice RM: 50 (16) A given implementation of the Random function in Numerics.Float_Random may or may not be capable of delivering the values 0.0 or 1.0. Portable applications should assume that these values, or values sufficiently close to them to behave indistinguishably from them, can occur. If a sequence of random integers from some fixed range is needed, the application should use the Random function in an appropriate instantiation of Numerics.Discrete_Random, rather than transforming the result of the Random function in Numerics.Float_Random. However, some applications with unusual requirements, such as for a sequence of random integers each drawn from a different range, will find it more convenient to transform the result of the floating point Random function. For M>=1, the expression 51 Integer(Float(M) * Random(G)) mod M 52 transforms the result of Random(G) to an integer uniformly distributed over the range 0 .. M-1; it is valid even if Random delivers 0.0 or 1.0. Each value of the result range is possible, provided that M is not too large. Exponentially distributed (floating point) random numbers with mean and standard deviation 1.0 can be obtained by the transformation 53 -Log(Random(G) + Float'Model_Small)) 54 where Log comes from Numerics.Elementary_Functions (see A.5.1); in this expression, the addition of Float'Model_Small avoids the exception that would be raised were Log to be given the value zero, without affecting the result (in most implementations) when Random returns a nonzero value.