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: James_Rogers Subject: Re: Random Number Generation Date: 1996/09/25 Message-ID: <32493D59.41C6@velveeta.apdev.cs.mci.com>#1/1 X-Deja-AN: 185269997 references: <5266q1$a0t@netty.york.ac.uk> content-type: text/plain; charset=us-ascii organization: MCI Telecommunications Colorado Springs, CO mime-version: 1.0 newsgroups: comp.lang.ada x-mailer: Mozilla 2.0 (X11; I; AIX 2) Date: 1996-09-25T00:00:00+00:00 List-Id: Nigel J. Tracey wrote: > > Can anyone point me in the direction of a routines to do > random number generation. The requirements are as follows. > > A routine which will generate a integer between a given > minimum and maximum value. Up to Integer'First and Integer'Last > > A routine which will do the same for floats i.e. a random > value from Float'First to Float'Last but between two > given values. > > The values of the min and max value for the above routines > are not known at compile time, hence why I don't thing > I can use Ada.Numerics.Discrete_Random > > Any help would be very much appreciated (please also e-mail > a copy of relies as the news feed is a little unreliable here) > > Thanks a lot, > The solution is really quite simple. Wrap your random number generator calls inside a function in the following manner: ------------------------------------------------------------- -- Flexible generic example ------------------------------------------------------------- with Ada.Numerics.Discrete_Random; with Ada.Text_IO; use Ada.Text_IO; procedure example is package int_io is new integer_io(integer); function flex_rand (Low, High : Integer) return Integer is subtype rand_type is Integer range Low..High; package myrand is new Ada.Numerics.Discrete_Random(rand_type); seed : myrand.generator; Result : rand_type; begin myrand.reset(seed); Result := myrand.random(seed); return Result; end flex_rand; Low, High : Integer; begin put("Enter the low bounds: "); int_io.get(Low); put("Enter the high bounds: "); int_io.get(High); put("Random number in the range of "); int_io.put(Low, Width => 1); put(" to "); int_io.put(High, Width => 1); put(" = "); int_io.put(flex_rand(Low, High), Width => 1); end example; ----------------------------------------------------------------- The wrapper function "flex_rand" provides all the flexibility you reqest. Of course, the same approach can be used for float types. -- Jim Rogers ************************************************************* Celebrate Diplomacy