comp.lang.ada
 help / color / mirror / Atom feed
* Random number generation
@ 2010-12-30 10:43 Mart van de Wege
  2010-12-30 10:54 ` Thomas Løcke
                   ` (4 more replies)
  0 siblings, 5 replies; 57+ messages in thread
From: Mart van de Wege @ 2010-12-30 10:43 UTC (permalink / raw)


Beginner's question: I'm trying to implement a function that rolls a
number of dice and then adds a modifier. Somehow, it produces the same
number every time. I can't see where I'm going wrong.

Here's the proof of concept code:

with Ada.Numerics.Discrete_Random,Ada.Integer_Text_IO;
use Ada.Integer_Text_IO;

procedure Rolltest is
   function Roll ( Number : in Positive;
		   Size : in Positive;
		   Modifier : in Integer := 0 ) return Integer is 
      subtype Die_Size is Positive range 2..Size;
      package Die is new Ada.Numerics.Discrete_Random( Die_Size );
      G : Die.Generator;
      Result : Integer;
      Temp : Integer;
   begin
      Die.Reset(G);
      Result := 0;
      for I in 1..Number loop
	 Temp := Die.Random(G);
	 Result := Result + Temp;
      end loop;
      Result := Result + Modifier;
      return Result;
   end Roll;
begin
   for I in 1..10 loop
      Put(Roll( Number => 3, Size => 6 ));
   end loop;
end Rolltest;

Anyone care to at least point me to some documentation that explains
what I'm doing wrong? 

Mart

-- 
"We will need a longer wall when the revolution comes."
    --- AJS, quoting an uncertain source.



^ permalink raw reply	[flat|nested] 57+ messages in thread
* Random number generation
@ 2010-07-13 12:45 tonyg
  2010-07-13 12:50 ` Jacob Sparre Andersen
                   ` (3 more replies)
  0 siblings, 4 replies; 57+ messages in thread
From: tonyg @ 2010-07-13 12:45 UTC (permalink / raw)


I want to generate a random integer and a random floating point number
between 10 and 30 . I've been looking at previous posts and finding it
a little confusing with many packages, has anyone an already done
example in ada 2005 they can post up?



^ permalink raw reply	[flat|nested] 57+ messages in thread
* Re: random number generation
@ 2003-09-26  7:14 christoph.grein
  0 siblings, 0 replies; 57+ messages in thread
From: christoph.grein @ 2003-09-26  7:14 UTC (permalink / raw)
  To: comp.lang.ada

> I need to write a program to randomly grab a integer from 1 - 4,and 
> output the result to the screen.  In some languages this would be a 
> fairly simple procedure.  Sometimes it is just a simple, single command 
> line to get the random number and one more to put the result to the 
> screen.
> 
> Does this exist in Ada?
>       I don't believe it does from what I have been reading.
>       But if it does please let me know.

RM A.5.2



^ permalink raw reply	[flat|nested] 57+ messages in thread
* random number generation
@ 2003-09-26  7:00 Andrew
  2003-09-26  7:35 ` tmoran
  0 siblings, 1 reply; 57+ messages in thread
From: Andrew @ 2003-09-26  7:00 UTC (permalink / raw)


I need to write a program to randomly grab a integer from 1 - 4,and 
output the result to the screen.  In some languages this would be a 
fairly simple procedure.  Sometimes it is just a simple, single command 
line to get the random number and one more to put the result to the 
screen.

Does this exist in Ada?
      I don't believe it does from what I have been reading.
      But if it does please let me know.




^ permalink raw reply	[flat|nested] 57+ messages in thread
* random number generation
@ 1997-12-19  0:00 Mok-kong Shen
  1998-01-02  0:00 ` Mok-kong Shen
  0 siblings, 1 reply; 57+ messages in thread
From: Mok-kong Shen @ 1997-12-19  0:00 UTC (permalink / raw)



Hello,

If you are interested in random number sequences, I like to invite you
to look at a combined pseudo-random number generator designed by me.
The underlying algorithm, which to my knowledge has not appeared
previously in the literature, is in fact extremely simple:

   j:=0; do output(G(j)); j:=trunc(n*G(j)) od;

where G(j) (0<=j<n) are n pseudo-random number generators (with output
in [0,1)) that can be arbitrarily given by the user, i.e. no particular
requirements of statistical qualities are imposed on them. These n
constituent generators can be of any kind; their parameters can be,
in particular, randomly chosen with the help of one additional pseudo-
random number generator.

The basic idea is the following: Each of the n constituent generators
produces an output stream that is, depending on its quality, more or
less random. If one randomly interleaves these streams, then the
result should be more random. In other words, the auto-correlations
that exist in the n individual output streams get lost through the
mixing process since elements that are at distance d (d>=1) apart
from one another in any particular one of the n output streams are no
longer at any fixed distance, say d', apart on the combined output.
Besides, half of the output of each generator is consumed internally
and does not appear as the output of the combined generator. From
the algorithm it can be seen that auto-correlations of elements at
distance d=1 apart of the n individual output streams are totally
lost on the combined output stream.

I found through experiments that the combined generator thus built
behaves, as expected, indeed very satisfactorily in both the frequency
and the serial test. Its qualities very quickly asymptotically improve
with larger n. Since the computing cost pro each output element of the
combined generator is independent of n, meaning that n can be chosen
to be fairly large without penality, I suppose that this scheme could
be very useful for practical applications.

Please give your comments and critiques. A well-documented Fortran 90 
implementation is available on

http://www.stud.uni-muenchen.de/~mok-kong.shen/

See also the attachment below.

------------------------------------------------------------
The following is a fragment taken from an old unpublished report of
mine. The experiment (referred to as 'a small-scale test run' in my Web
document) was done at a time when the PCs were of 20MHZ, hence the test
sequences were short. Later experiments confirmed the superior
statistical behaviour previously found of my algorithm. (Note that the
generation of the constituent generators in the computer program
provided in the Web document is done somewhat differently from what is
described below.)


   In order to quantitatively verify the performance of our algorithm as
predicted from the theoretical considerations above, an experiment was
done on a set of generators that were each internally based on a LCPRNG
of the mixed type. The coefficients of these LCPRNG's were themselves
generated by a random number generator named RANDOM provided by the UNIX
operating system. In order to obtain large periods of the sequences
generated, we posed the requirement that the modulus of each LCPRNG be a
prime lying between 2**18 and 2**30. Otherwise, all coefficients of the
congruence relations involved were randomly chosen, subject to the
single necessary constraint that no arithmetic overflow should ever
occur on a computer of 32 bit word length. The sequence of integers
obtained from each LCPRNG were divided by the corresponding modulus so
that the output of each generator was real numbers in the range [0,1).
We obtained in this manner 12 generators to serve as constituent
generators of our scheme. Then we applied our algorithm for n=1 to the
first generator obtained. The system output was multiplied by 32 and
then truncated so that a sequence of random integers lying between 0 and
31 was obtained. On this sequence, of length 10000, the serial test
(non-overlapping) was applied, leading to the values of the test
statistic given for n=1 in the table below for three different values of
the distance u. Next we applied our algorithm for n=2 to the set
consisting of the first and the second generator, leading to the values
of the test statistic given for n=2 in the table. The other entries of
the table were obtained in analogous fashion.

   Note that for n=1 and u=1 the value of the test statistic obtained is
very large. In fact, even larger values were obtained with the serial
test applied directly to the output of any of the 12 randomly chosen
random number generators (test done on integer sequences in [0,31] as
above). This is in conformance to the well-known fact that it is
extremely difficult to find coefficients for a LCPRNG such that its
output is satisfactory in respect of statistical correlations. For n>=9
the test statistic of our combined generator has apparently attained
values that cannot be further improved, i.e. values that would also have
been obtained even from truly random sequences.

             n     u=1      u=2      u=3
             1   31251.6   5740.9   1104.2
             2    8641.3   3402.1   1706.0
             3    3158.0   1674.8   1354.6
             4    1917.3   1264.4   1198.1
             5    1610.9   1252.1   1114.1
             6    1489.7   1139.5   1096.0
             7    1461.9   1144.0   1090.7
             8    1362.7   1044.9   1106.3
             9    1185.0   1162.9   1043.2
            10    1155.9    975.2   1022.7
            11    1155.5   1016.6   1001.4
            12    1085.8   1067.0   1070.6

   For comparison purposes, we also ran the serial test (in the same
manner as above) directly on the output of the generator RANDOM of the
UNIX operating system, the generator G05CAF of the NAG library and the
generator RNUNF of the IMSL library. The values of the test statistic
obtained are given below. It can be clearly seen by comparing the two
tables that through combining some 10 fairly bad random number
generators according to our algorithm we have succeeded to obtain output
sequences whose quality is comparable to that of the three well-known
good random number generators examined.

                    u=1      u=2      u=3
          RANDOM   1001.1    978.9    979.7
          G05CAF   1078.5   1033.4   1036.2
          RNUNF    1028.1   1016.2    947.3




^ permalink raw reply	[flat|nested] 57+ messages in thread
* Re: Random Number Generation
@ 1996-10-13  0:00 parker
  1996-10-13  0:00 ` Robert Dewar
  1996-10-14  0:00 ` Robert A Duff
  0 siblings, 2 replies; 57+ messages in thread
From: parker @ 1996-10-13  0:00 UTC (permalink / raw)




Geert Bosch wrote:

"I don't say it is not possible to present a correct algorithm
to convert R1 to R2. Such a correct algorithm just is not guaranteed to
finish in finite time.

Your algorithm:
`` x := random;
   while x > R1'Last - 1024 rem 3 loop
      x := random;
   end loop;
   return x rem 3; ''

You can't give an upper boundary for the number of iterations
of the while loop if R1 is a truly random generator.

Of course in practise you're only interested in having an
algorithm that has a good probability to finish in a certain
amount of time.

I just pointed out that the example wasn't that simple.
Somebody using this code should be aware of that. "


Now I see what you're getting at.
I wouldn't worry about it tho.....overhead from M rejections goes linearly
with M (M calls to R1), probability of M rejections goes as 2**(-M*10)...

Keith and Geert are concerned about fluctuating overhead in the rejection
method.  I like Keith's solution: an upper limit on the number of
rejections.  In this case, if the upper limit is set to 2, then the error
is undetectable: ie if you reject 2 1023's in a row, then accept whatever 
comes next; the offending 3 1023's come with a probability of 2**(-30)
for an undetectable bias.  So max overhead is 3 calls to R1, average
overhead is 1.001 calls to R1 (plus the if, and the rem).

An alterative with same error, but constant overhead: make R1 into
the range 0..2**30-1. iTo do this think of R1 as returnig a single
digit number base 1024; use 3 calls to R1 to make x, 
a 3 digit number  base 1024.  Now
just return x rem 3: same bias, same max overhead as other
method, but 3 times the average overhead.

cheers, Jonathan





^ permalink raw reply	[flat|nested] 57+ messages in thread
* Re: Random Number Generation
@ 1996-10-10  0:00  Dr J Parker
  1996-10-12  0:00 ` Geert Bosch
  1996-10-12  0:00 ` Keith Thompson
  0 siblings, 2 replies; 57+ messages in thread
From:  Dr J Parker @ 1996-10-10  0:00 UTC (permalink / raw)



Jonathan Parker (jp3@rl.ac.uk) wrote:
-`` Another simple example:
-
-   Suppose you have a high quality random number generator R1 in the range
-   0 .. 1023 and you want to turn it into a high quality random number
-   generator R2 in the range 0 .. 2. ''
-
-This is not a simple example. It is not possible to write an
-Ada program that does convert R1 to R2 and is guaranteed to
-finish in finite time.
-
-Regards,
-   Geert
--
-E-Mail: geert@sun3.iaf.nl


Nah... the rejection method is rigorously correct, and so is
the subsequent use of x rem 3 in the code fragment below.

The original post never reached some newsreaders so I repeat it below.
btw, the answer to the ? below is about N = 10**7, about 10 secs
on a PC.  If R1 has range
0..2**15-1 instead then it takes about an hour to detect the bias.
The method Tucker suggests is one of best, provided
you know how it can fail and you know how to avoid it.

cheers, Jonathan


*******************************************************8
Another Simple example:

Suppose you have a high quality random number generator R1 in the range
0 .. 1023
and you want to turn it into a high quality random number generator R2
in the range 0 .. 2.

R1 has 8 more bits than R2.

Suppose you try R2  =  R1 / 342.

this maps
0..341    to 0,
342..683  to 1
684..1023 to 2

0 has probability 342/1024, 1 has probability 342/1024, and
2 has probability 340/1024.

You can use  R1 mod 3, get same problem.
You can use  R1 * 3.0, with floating point R1 in [0.0..1.0)
and you get same problem if R1 has 1024  elements in its range.

Suppose you think R2 is uniformly distributed in 0,1, 2.
How many trials (N) would it take to detect the bias?

In under 5 miinutes you can generate N = 3 * 10**8 random numbers on
a PC.  expect 10**8 0's, 10**8 1's 10**8 2's , with stnd deviation
about 10**4 and error = 10**8 * 1.333/1024.0.

You get wrong answer by about 10 stnd deviations!

And all you had to do was reject number 1023 from the range of R2!
(because the number of elements in the range of 0..1022 is
divisible by 3.)

x := random;
while x > R1'Last - 1024 rem 3 loop
   x := random;
end loop;
return x rem 3;
*********************************************************




^ permalink raw reply	[flat|nested] 57+ messages in thread
* Re: Random Number Generation
@ 1996-10-10  0:00  Dr J Parker
  0 siblings, 0 replies; 57+ messages in thread
From:  Dr J Parker @ 1996-10-10  0:00 UTC (permalink / raw)



I wrote

-In under 5 miinutes you can generate N = 3 * 10**8 random numbers on
-a PC.  expect 10**8 0's, 10**8 1's 10**8 2's , with stnd deviation
-about 10**4 and error = 10**8 * 1.333/1024.0.

meaning of course:

error = 3 * 10**8 * 1.333/1024.0 in one of the 3 bins.






^ permalink raw reply	[flat|nested] 57+ messages in thread
* Re: Random Number Generation
@ 1996-10-02  0:00  Dr J Parker
  1996-10-03  0:00 ` Mats Weber
  1996-10-07  0:00 ` Geert Bosch
  0 siblings, 2 replies; 57+ messages in thread
From:  Dr J Parker @ 1996-10-02  0:00 UTC (permalink / raw)



Another simple example:

Suppose you have a high quality random number generator R1 in the range
0 .. 1023
and you want to turn it into a high quality random number generator R2
in the range 0 .. 2.

R1 has 8 more bits than R2.

Suppose you try R2  =  R1 / 342.

this maps
0..341    to 0,
342..683  to 1
684..1023 to 2

0 has probability 342/1024, 1 has probability 342/1024, and
2 has probability 340/1024.

You can use  R1 mod 3, get same problem.
You can use  R1 * 3.0, with floating point R1 in [0.0..1.0)
and you get same problem if R1 has 1024  elements in its range.

Suppose you think R2 is uniformly distributed in 0,1, 2.
How many trials (N) would it take to detect the bias?

In under 5 miinutes you can generate N = 3 * 10**8 random numbers on
a PC.  expect 10**8 0's, 10**8 1's 10**8 2's , with stnd deviation
around 10**4 and error = 3 * 10**8 * 1.333/1024.0.

You get wrong answer by about 30 stnd deviations!

And all you had to do was reject number 1023 from the range of R2!
(because the number of elements in the range of 0..1022 is
divisible by 3.)

x := random;
while x > R1'Last - 1024 rem 3 loop
   x := random;
end loop;
return x rem 3;





^ permalink raw reply	[flat|nested] 57+ messages in thread
* Random Number Generation
@ 1996-09-23  0:00 Nigel J. Tracey
  1996-09-23  0:00 ` Tucker Taft
                   ` (2 more replies)
  0 siblings, 3 replies; 57+ messages in thread
From: Nigel J. Tracey @ 1996-09-23  0:00 UTC (permalink / raw)



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,

	Nigel

--------------------
Nigel J. Tracey MEng, Research Associate (High Integrity Systems Group).
Department of Computer Science,   Office: X/D016
University of York,               Tel   : [0|+44]1904 432769
York, England.                    E-Mail: njt@minster.york.ac.uk
YO1 5DD.                          URL   : http://sv1pc161.cs.york.ac.uk/~njt




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

end of thread, other threads:[~2010-12-31  3:14 UTC | newest]

Thread overview: 57+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-12-30 10:43 Random number generation 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
  -- strict thread matches above, loose matches on Subject: below --
2010-07-13 12:45 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
2003-09-26  7:14 random " christoph.grein
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
1997-12-19  0:00 Mok-kong Shen
1998-01-02  0:00 ` Mok-kong Shen
1998-01-02  0:00   ` Robert Dewar
1996-10-13  0:00 Random Number Generation parker
1996-10-13  0:00 ` Robert Dewar
1996-10-14  0:00 ` Robert A Duff
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-02  0:00  Dr J Parker
1996-10-03  0:00 ` Mats Weber
1996-10-07  0:00 ` Geert Bosch
1996-09-23  0:00 Nigel J. Tracey
1996-09-23  0:00 ` Tucker Taft
1996-10-02  0:00   ` Robert I. Eachus
1996-10-02  0:00   ` Nigel J. Tracey
1996-10-03  0:00   ` Nigel J. Tracey
1996-09-25  0:00 ` James_Rogers
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

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