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-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  4:08 ` Andy Starritt
  2003-01-08 20:56 ` Stephen Leake
  2 siblings, 1 reply; 7+ messages in thread
From: tmoran @ 2003-01-08  1:55 UTC (permalink / raw)


> sometimes sees the value 20 and on other occasions doesn't.

You are generating 30 random integer from 1 ..  50 (15 from the
"Ada.Integer_Text_IO.Put" and another 15 from the
"if Random_50.Random(Gen => G) = 20 then").  The probability that
none of those is a 20 is (49/50)**30 = 0.55   If the 20 should come
up on the "Ada.Integer_Text_IO.Put" you will see it printed
out.  Since those are half of the 30 calls, you ought to see "20"
printed about 0.27 of the time.  If the 20 comes up on the
"if" statement instead, you won't see a 20 at all, but will instead
see "Random Number is equal to Twenty".  That also should happen
on about 27% of the runs.  Running your program 100 times, a quarter
second apart (to let the random generator start differently), I
see 26 of the runs output "Random Number is equal to Twenty" at
least once.  Sounds just about right.



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

* Re: Pseudo Random coding problem
  2003-01-07 23:47 Pseudo Random coding problem Brian A Crawford
  2003-01-08  1:55 ` tmoran
@ 2003-01-08  4:08 ` Andy Starritt
  2003-01-08 20:56 ` Stephen Leake
  2 siblings, 0 replies; 7+ messages in thread
From: Andy Starritt @ 2003-01-08  4:08 UTC (permalink / raw)


brianc@billybob.demon.co.uk (Brian A Crawford) wrote in message news:<3e1b633f.3280604@news.demon.co.uk>...

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

Because your program loops only 15 times, and the random number
range is 1 to 50, I would expect 20 to turn up (on average) approx 
30% of the time.  It is supposed to be random after all ;-)


> 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  1:55 ` tmoran
@ 2003-01-08 10:57   ` Brian A Crawford
  2003-01-08 13:45     ` David C. Hoos
  0 siblings, 1 reply; 7+ messages in thread
From: Brian A Crawford @ 2003-01-08 10:57 UTC (permalink / raw)


My intention was to produce one stream of pseudo random numbers
continuously one at a time (until say 10 billion trials) for a
simulation; look at that number produced; 
if it was a 20 then do one action, 
if it was a 5 or a 10 do another, 
if it was a 2, 4, 6, 8 do another. 
 
This is simplified somewhat.  I did it in the range of 1 to 50 and 15
trials just for debugging and checking code.  
The random range will be 1.. 1Million.
The number 20, 2, 5 etc aren't important; it was an easy way of
getting a probability of events happening.
E.g #2,4,6,8 coming up in 1 million numbers is 250K to 1 etc.

According to the replies so far I seem to be producing two streams.
How do I produce and check one only.

Many Thanks 
Brian

On Wed, 08 Jan 2003 01:55:07 GMT, tmoran@acm.org wrote:

>> sometimes sees the value 20 and on other occasions doesn't.
>
>You are generating 30 random integer from 1 ..  50 (15 from the
>"Ada.Integer_Text_IO.Put" and another 15 from the
>"if Random_50.Random(Gen => G) = 20 then").  The probability that
>none of those is a 20 is (49/50)**30 = 0.55   If the 20 should come
>up on the "Ada.Integer_Text_IO.Put" you will see it printed
>out.  Since those are half of the 30 calls, you ought to see "20"
>printed about 0.27 of the time.  If the 20 comes up on the
>"if" statement instead, you won't see a 20 at all, but will instead
>see "Random Number is equal to Twenty".  That also should happen
>on about 27% of the runs.  Running your program 100 times, a quarter
>second apart (to let the random generator start differently), I
>see 26 of the runs output "Random Number is equal to Twenty" at
>least once.  Sounds just about right.




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

* Re: Pseudo Random coding problem
  2003-01-08 10:57   ` Brian A Crawford
@ 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

* 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

* Re: Pseudo Random coding problem
  2003-01-07 23:47 Pseudo Random coding problem Brian A Crawford
  2003-01-08  1:55 ` tmoran
  2003-01-08  4:08 ` Andy Starritt
@ 2003-01-08 20:56 ` Stephen Leake
  2 siblings, 0 replies; 7+ messages in thread
From: Stephen Leake @ 2003-01-08 20:56 UTC (permalink / raw)


brianc@billybob.demon.co.uk (Brian A Crawford) writes:

> I am trying to start a small programming project that will output
> pseudo-random numbers.

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

This makes your tests non-repeatable, which is why you sometimes see
20 and sometimes don't. You probably want to do 

Random_50.Resest (Gen => G, Initiator => 0);

to get repeatable results.

In your final application, you may want to start from the time of day,
but probably not.

-- 
-- Stephe



^ 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