comp.lang.ada
 help / color / mirror / Atom feed
* Pseudo Random coding problem
@ 2003-01-07 23:47 Brian A Crawford
  2003-01-08  1:55 ` tmoran
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Brian A Crawford @ 2003-01-07 23:47 UTC (permalink / raw)


I am trying to start a small programming project that will output
pseudo-random numbers.
The program looks at the numbers produced and if that number equals a
predetermined value (20 in the case in the test program below) the
program will then catch this fact and perform a number of loops and
counts etc.

However I've fallen at the first hurdle.  I've heavily borrowed code
from Ada95 by Feldman/Koffman (thankyou!) and find that the program
sometimes sees the value 20 and on other occasions doesn't.
It's very tempermental. 
The code is below.  Ive definitely missed something.    
________________________________________________

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;   
   
   BEGIN -- Random_Numbers
   
      Random_50.Reset (Gen => G);  -- starts G from time of day clock

      for row in 1..15 loop
      
         Ada.Integer_Text_IO.Put
               (Item => Random_50.Random(Gen => G), Width => 8);
      
         if  Random_50.Random(Gen => G) = 20 then
            Ada.Text_io.put (Item => "Random Number is equal to
Twenty");

            Ada.text_io.new_line;
         end if;
         Ada.text_io.new_line;
      
      end loop;
   
   END Random_Numbers;
_______________________________________________________




^ permalink raw reply	[flat|nested] 7+ messages in thread
* Re: Pseudo Random coding problem
@ 2003-01-08 13:45 David C. Hoos
  0 siblings, 0 replies; 7+ messages in thread
From: David C. Hoos @ 2003-01-08 13:45 UTC (permalink / raw)



"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>






^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2003-01-08 20:56 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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