comp.lang.ada
 help / color / mirror / Atom feed
From: "David C. Hoos" <david.c.hoos.sr@ada95.com>
Subject: Re: Pseudo Random coding problem
Date: Wed, 8 Jan 2003 07:45:33 -0600
Date: 2003-01-08T07:45:33-06:00	[thread overview]
Message-ID: <v1oartsu83oid3@corp.supernews.com> (raw)
In-Reply-To: 3e1c00e7.634181@news.demon.co.uk


"Brian A Crawford" <brianc@billybob.demon.co.uk> wrote in message
news:3e1c00e7.634181@news.demon.co.uk...
<snip>
> According to the replies so far I seem to be producing two streams.
> How do I produce and check one only.
You are generating only one stream (because you are using only one
generator,
initialized only once).  However, you are sampling the stream from two
points in
your code, thus creating two "substreams," as it were.

So, the key is to assign the value of the draw to a variable, and then make
whatever tests you wish on that one draw, before drawing again.  Each call
to
Random_50.Random is a "draw."

Here's a refinement of your program which shows a better way to test for
randomness:

with Ada.Text_IO;
with Ada.Integer_Text_IO;
with Ada.Float_Text_IO;
with Ada.Numerics.Discrete_Random;

procedure Random_Numbers is

   subtype Randomrange is Positive range 1..50;

   package Random_50 is new Ada.Numerics.Discrete_Random
     (Result_Subtype => Randomrange);


   G: Random_50.Generator;

   Draw : Randomrange;
   Occurrence_Array : array (Randomrange) of Natural := (others => 0);
   Minimum : Natural := Natural'Last;
   Maximum : Natural := Natural'First;
begin -- random_numbers

      Random_50.Reset (Gen => G);  -- starts g from time of day clock

      for Row in 1 .. 100_000_000 loop
         Draw := Random_50.Random (Gen => G);
         Occurrence_Array (Draw) := Occurrence_Array (Draw) + 1;
      end loop;

      for O in Occurrence_Array'Range loop
         Ada.Text_IO.Put_Line
           ("The value" & Randomrange'Image (O) & " occurred" &
            Natural'Image (Occurrence_Array (O)) & " times.");
         Minimum := Natural'Min (Minimum, Occurrence_Array (O));
         Maximum := Natural'Max (Maximum, Occurrence_Array (O));
      end loop;

      Ada.Text_IO.Put_Line
        ("The minimum number of occurrences of any one value was" &
         Natural'Image (Minimum));
      Ada.Text_IO.Put_Line
        ("The maximum number of occurrences of any one value was" &
         Natural'Image (Maximum));
end Random_Numbers;

The last two lines of the program output from two runs were:

Run 1:
The minimum number of occurrences of any one value was 1995646
The maximum number of occurrences of any one value was 2003366

Run 2:
The minimum number of occurrences of any one value was 1996974
The maximum number of occurrences of any one value was 2003924

<snip>





  reply	other threads:[~2003-01-08 13:45 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-01-07 23:47 Pseudo Random coding problem Brian A Crawford
2003-01-08  1:55 ` tmoran
2003-01-08 10:57   ` Brian A Crawford
2003-01-08 13:45     ` David C. Hoos [this message]
2003-01-08  4:08 ` Andy Starritt
2003-01-08 20:56 ` Stephen Leake
  -- strict thread matches above, loose matches on Subject: below --
2003-01-08 13:45 David C. Hoos
replies disabled

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