Random long-number generator

Herwig Henseler henseler at uniol.UUCP
Fri Dec 22 23:04:12 AEST 1989


Hello World,

There seems to be no regular posting 'Answers to frequently asked questions'
in comp.lang.c. Here is the _best_ (!) random number generator dealing with
(long). (You can find the proof in 'Communications of ACM' 1988; I don't
know the exact number, sorry)

-------------------------------- cut here -----------------------------------
#define a_random        (long) 16807
#define m_random        (long) 2147483647
#define q_random        (long) 127773           /* m div a */
#define r_random        (long) 2836             /* m mod a */

long seed;

/* Return random value [0..1) */

double
random()
{
	seed = a_random * (seed % q_random) - r_random * (seed / q_random);
	if( seed <= 0 )
		seed += m_random;
	return( (double) seed / (double) m_random );
}

main()
{
	/* Initialize seed */
	(void) time(&seed);
	seed += getpid();
	random();

	while(1) {
		printf( "%lf\n", random() );
	}
}
-------------------------------- cut here -----------------------------------

I hope this will prevent further questions for a few months.

	bye, Herwig
--
## Herwig Henseler (CS-Student) D-2930 Varel, Tweehoernweg 69 | Brain fault- ##
## EMail: henseler at uniol.UUCP (..!uunet!unido!uniol!henseler) | core dumped  ##



More information about the Comp.lang.c mailing list