J
jacob navia
Richard said:jacob navia said:
Show us.
This program:
---------------------------------------------------------------
/*
* Pseudo-random number generator. The result is uniform on
* [0, 2^31 - 1].
*/
static unsigned long _randseed = 1;
unsigned long random(void)
{
register long x, hi, lo, t;
/*
* Compute x[n + 1] = (7^5 * x[n]) mod (2^31 - 1).
* From "Random number generators: good ones are hard to find",
* Park and Miller, Communications of the ACM, vol. 31, no. 10,
* October 1988, p. 1195.
*/
x = _randseed;
hi = x / 127773;
lo = x % 127773;
t = 16807 * lo - 2836 * hi;
if (t <= 0)
t += 0x7fffffff;
_randseed = t;
return (t);
}
void srandom(unsigned long seed)
{
_randseed=seed;
}
#include <stdio.h>
int main(void)
{
for (int i=0; i<10;i++) {
printf("%d\n",random()&0xffff);
}
}
-----------------------------------------------------------------------
produces the following output:
16807
15089
44249
3114
46978
56008
36568
2558
12099
1101
(1)
I am of course unable to write a fully compliant C99 compiler as you
say, but I can still read a list of numbers.
Your assertion that the random number generator produces always the
same result is FALSE.
(2) I can't tell you if the properties of those numbers are the same as
the 32 bit numbers. I *know* that this algorithm has been ported to a
16 bit DSP, but I can't tell you its equivalent in standard C.
(3) For a very good discussion about the history of this algorithm,
random numbers, and related topics see
http://www.firstpr.com.au/dsp/rand31/