Topic: True random number generator Wanted
Author: hendrik@vedge.com (Hendrik Boom)
Date: 22 Mar 93 14:16:09 GMT Raw View
paulf@htsa.aha.nl (Paul Ferron) writes:
: could anybody out here mail me a true random number generator algorithm.
:
: I've found that the random number generators in C++ are not al that random.
:
: thanx
:
: Paul
:
: --
: --------------------------------------------------------------------------------
: Kevin Landwaster son of Lorric No smart comments are mine.
: Arch-mage of Threefork. aka paulf@solist.htsa.aha.nl
: --------------------------------------------------------------------------------
This comes up so often that a solution should perhaps be in the FAQ.
Here's a copy of random.c as I used it in one of
my programs, together with reference s to the original article,
(which you *should* read -- it says a lot about
the care and feeding of this --and other -- random number generaE
tors.
#if 0
Ths following are the original random variations from
Random Number Generators: Good ones are hard to find,
by Stephen K. Park and Keith W. Miller,
in Communications of the ACM, October 1988 Volume 31 Number 10,
pages 1192 - 1201.
function Random : real;
(* Integer version 1: maxint must be 2**46-1 or larger *)
const
a = 16807;
m = 2147483647;
begin
seed := (a * seed) mod m;
Random := seed / m
end;
correctness: The following should yield 1043618065
seed := 1;
for n := 1 to 10000 do
u := Random;
Writeln('The current value of seed is : ', seed);
var seed : real;
function Random : real;
(* real version 1: required 46-bit or larget mantissa
(excluding the sign bit)
*)
const
a = 16807.0;
m = 2147483647.0;
var
temp : real;
begin
temp := a * seed;
seed := temp - m * Trunc(temp / m);
Random := seed / m
end;
function Random : real;
(* Integer Version 2 : required maxint of 2**31 or larger *)
const
a = 16807;
m = 2147483647;
q = 127773; (* m div a *)
r = 2836; (* m mod a *)
var
lo, hi, test : integer;
begin
hi := seed div q;
lo := seed mod q;
test := a * lo - r * hi;
if test > 0 then
seed := test
else
seed := test + m;
Random := seed / m
end;
function random : real;
(* Real version 2: requires 32-bit or larger mantissa;
requires maxint at least 16807
*)
const
a = 16807.0;
m = 2147483647.0;
q = 127773.0; (* m div a *)
r = 2836.0; (* m div a *)
var
lo, hi, test : real;
begin
hi := Trunc(seed / q);
lo := seed - q * hi;
test := a * lo - r * hi;
if test > 0.0 then
seed := test
else
seed := test + m;
Random := seed / m
end;
TurboPascal version 3.0 needs 'real version 2' of random.
The '87' version of TurboPascal can use either 'real version'.
#endif
long seed = 1;
double Random(Void)
/* Integer Version 2 : required maxint of 2**31 or larger */
#define a 16807
#define m 2147483647
#define q 127773
/* m div a */
#define r 2836
/* m mod a */
{ long int lo, hi, test;
hi = seed / q;
lo = seed % q;
test = a * lo - r * hi;
if(test > 0)
seed = test;
else
seed = test + m;
return ((double)seed) / ((double)m);
}
main()
{ int i;
double u;
for(i = 1; i <= 10000; i++)
{ u = Random();
}
printf("\nThe current value of seed is %ld.", seed);
printf("\nIt should be 1043618065.\n");
}
Hendrik Boom CAM.ORG!ozrout!topoi!hendrik
4133 Marcil hendrik@topoi.uucp
Montreal topoi!hendrik@uunet.uu.net
topoi!hendrik@vedge.com
vedge!topoi!hendrik@sobeco.com
--
-------------------------------------------------------
Try one or more of the following addresses to reply.
at work: hendrik@vedge.com, iros1!vedge!hendrik
at home: uunet!ozrout!topoi!hendrik
Author: paulf@htsa.aha.nl (Paul Ferron)
Date: Fri, 19 Mar 1993 09:29:39 GMT Raw View
could anybody out here mail me a true random number generator algorithm.
I've found that the random number generators in C++ are not al that random.
thanx
Paul
--
--------------------------------------------------------------------------------
Kevin Landwaster son of Lorric No smart comments are mine.
Arch-mage of Threefork. aka paulf@solist.htsa.aha.nl
--------------------------------------------------------------------------------