comp.lang.ada
 help / color / mirror / Atom feed
From: James_Rogers <jrogers@velveeta.apdev.cs.mci.com>
Subject: Re: Random Number Generation
Date: 1996/09/25
Date: 1996-09-25T00:00:00+00:00	[thread overview]
Message-ID: <32493D59.41C6@velveeta.apdev.cs.mci.com> (raw)
In-Reply-To: 5266q1$a0t@netty.york.ac.uk


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




  parent reply	other threads:[~1996-09-25  0:00 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-09-23  0:00 Random Number Generation Nigel J. Tracey
1996-09-23  0:00 ` Tucker Taft
1996-10-02  0:00   ` Nigel J. Tracey
1996-10-02  0:00   ` Robert I. Eachus
1996-10-03  0:00   ` Nigel J. Tracey
1996-09-25  0:00 ` James_Rogers [this message]
1996-09-26  0:00   ` Dale Stanbrough
1996-10-01  0:00   ` Robert I. Eachus
1996-09-30  0:00 `  Dr J Parker
1996-10-01  0:00   ` Tucker Taft
1996-10-01  0:00     ` Keith Thompson
  -- strict thread matches above, loose matches on Subject: below --
1996-10-02  0:00  Dr J Parker
1996-10-03  0:00 ` Mats Weber
1996-10-07  0:00 ` Geert Bosch
1996-10-10  0:00  Dr J Parker
1996-10-12  0:00 ` Geert Bosch
1996-10-12  0:00 ` Keith Thompson
1996-10-10  0:00  Dr J Parker
1996-10-13  0:00 parker
1996-10-13  0:00 ` Robert Dewar
1996-10-14  0:00 ` Robert A Duff
1997-12-19  0:00 random number generation Mok-kong Shen
1998-01-02  0:00 ` Mok-kong Shen
1998-01-02  0:00   ` Robert Dewar
2003-09-26  7:00 Andrew
2003-09-26  7:35 ` tmoran
2003-09-26 17:58   ` Andrew
2003-09-26 19:25   ` Andrew
2003-09-26 19:35     ` chris
2003-09-26 21:44     ` tmoran
2003-09-27  1:40     ` Robert I. Eachus
2003-09-27  4:48       ` Andrew
2003-09-26  7:14 christoph.grein
2010-07-13 12:45 Random " tonyg
2010-07-13 12:50 ` Jacob Sparre Andersen
2010-07-13 12:58 ` Dmitry A. Kazakov
2010-07-13 13:17 ` Thomas Løcke
2010-07-13 16:07 ` Jeffrey R. Carter
2010-07-13 20:33   ` John B. Matthews
2010-07-13 23:02     ` Jeffrey R. Carter
2010-07-14  4:42       ` John B. Matthews
2010-07-15 19:01         ` tonyg
2010-12-30 10:43 Mart van de Wege
2010-12-30 10:54 ` Thomas Løcke
2010-12-30 12:11   ` Mart van de Wege
2010-12-30 11:34 ` Niklas Holsti
2010-12-30 11:53   ` Georg Bauhaus
2010-12-30 12:25     ` Mart van de Wege
2010-12-30 15:29       ` Georg Bauhaus
2010-12-30 15:37         ` Mart van de Wege
2010-12-30 11:51 ` Brian Drummond
2010-12-30 12:16   ` Mart van de Wege
2010-12-30 13:04 ` Dmitry A. Kazakov
2010-12-30 13:22   ` Niklas Holsti
2010-12-30 13:39     ` Dmitry A. Kazakov
2010-12-30 13:30   ` Mart van de Wege
2010-12-31  3:14 ` Gene
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox