Pseudo Random coding problem 
Author Message
 Pseudo Random coding problem

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;
_______________________________________________________



Sun, 26 Jun 2005 07:47:50 GMT  
 Pseudo Random coding problem

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


Sun, 26 Jun 2005 09:55:07 GMT  
 Pseudo Random coding problem

Quote:
> 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 ;-)
Quote:
> 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;
> _______________________________________________________



Sun, 26 Jun 2005 12:08:18 GMT  
 Pseudo Random coding problem
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

Quote:

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



Sun, 26 Jun 2005 18:57:54 GMT  
 Pseudo Random coding problem



<snip>

Quote:
> 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>



Sun, 26 Jun 2005 21:45:33 GMT  
 Pseudo Random coding problem



<snip>

Quote:
> 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>



Sun, 26 Jun 2005 21:45:33 GMT  
 Pseudo Random coding problem

Quote:
> 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



Mon, 27 Jun 2005 04:56:14 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Writing assembler code/pseudo code

2. R250 Pseudo-random number generator

3. Pseudo-random numbers

4. How can I randomize the pseudo-random sequence?

5. Pseudo Random Generator

6. Generating (pseudo)random number sequences

7. Reverse pseudo-random

8. pseudo random counter

9. Help: Pseudo-random Sequenece Verification

10. A Simple and Efficient Pseudo-Random Bit Generator

11. Link for MLS pseudo random number generator polynomials

12. Excellent pseudo random number generator

 

 
Powered by phpBB® Forum Software