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.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: "Jeffrey R. Carter" Newsgroups: comp.lang.ada Subject: Re: Ada.Numerics.Float_Random.Generator question Date: Sat, 17 Sep 2016 14:01:26 -0700 Organization: Also freenews.netfront.net; news.tornevall.net; news.eternal-september.org Message-ID: References: <5db08ea5-c088-4d16-9d88-d058e4bc48df@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Injection-Date: Sat, 17 Sep 2016 21:01:22 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="6df4b173985f7c5c043cea362c370ff7"; logging-data="11066"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+PVuTNquIX/GTvS8dT7ozC3TzJNuYGCjQ=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 In-Reply-To: <5db08ea5-c088-4d16-9d88-d058e4bc48df@googlegroups.com> Cancel-Lock: sha1:u12uVsjaS3BcugMhdZpA7xwxFmk= Xref: news.eternal-september.org comp.lang.ada:31803 Date: 2016-09-17T14:01:26-07:00 List-Id: On 09/17/2016 12:40 PM, Andrew Shvets wrote: > Hello, > > If I want a random float number to be generated, the way that I did this in the past was something along these lines: > > function Create_Random_Float( > From : in Float; > To : in Float) > return Float is > > Seed : Ada.Numerics.Float_Random.Generator; > begin > Ada.Numerics.Float_Random.Reset(Seed); > > return From + (Ada.Numerics.Float_Random.Random(Seed) * To); > end Create_Random_Float; > > This works. No, it doesn't. If From is 5 and To is 10, This gives a value in 5 .. 15. However, as I'm now looking into the Ada.Numerics.Discrete_Random package, I've noticed that it can generate a random value based on the type that is passed in. This appeals to me and I'd like to do the same for a custom float type (say I want the delta to be 0.01.) > > As Ada.Numerics.Float_Random.Generator is not a generic package, I can't use the same approach. The best way that I can think of is to create the same result using From and To and then cast the result to the custom float type. The PragmAda Reusable Components contain several RNGs, all of which can be used to obtain random values of user-defined floating-point types. You can find them at https://github.com/jrcarter/PragmARC -- Jeff Carter "C++ is like jamming a helicopter inside a Miata and expecting some sort of improvement." Drew Olbrich 51