Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!uwm.edu!zaphod.mps.ohio-state.edu!tut.cis.ohio-state.edu!ucbvax!bloom-beacon!eru!luth!sunic!mcsun!unido!uniol!henseler From: henseler@uniol.UUCP (Herwig Henseler) Newsgroups: comp.lang.c Subject: Re: Random long-number generator Keywords: random maxint Message-ID: <1540@uniol.UUCP> Date: 22 Dec 89 12:04:12 GMT References: <83943@linus.UUCP> <1486@skye.ed.ac.uk> Organization: University of Oldenburg, W-Germany Lines: 45 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@uniol.UUCP (..!uunet!unido!uniol!henseler) | core dumped ##