455,515 Members | 1,848 Online
Need help? Post your question and get tips & solutions from a community of 455,515 IT Pros & Developers. It's quick & easy.

# PRNG Algorithm for RAN()

 P: n/a Does anybody know what pseudo-random number generator was used to implement the RAN() function in the C standard math library? Knuth couldn't have gone so far as to implement a quadratic congruential method, right? Is it yet another implementation of the much reviled RANDU? Nov 13 '05 #1
4 Replies

 P: n/a John Schutkeker wrote in message news:Xn******************************@205.188.138. 161... Does anybody know what pseudo-random number generator was used to implement the RAN() function in the C standard math library? There isn't a single one. Each implementation is free to use its own version, as long as its behavior meets specifications. Knuth couldn't have gone so far as to implement a quadratic congruential method, right? I don't know, why not look it up? Is it yet another implementation of the much reviled RANDU? The C standard does not specify which algorithm be used by the standard library function 'rand()'. -Mike Nov 13 '05 #2

 P: n/a John Schutkeker wrote: Does anybody know what pseudo-random number generator was used to implement the RAN() function in the C standard math library? There is no RAN() function in the C library (and if there were, that all-caps name would be a bad idea). The Standard RNG function is called rand(). rand() is not in the math library, nor in the maths headers, but in and in "the" library. There is no required algorithm for rand(); all it has to do is provide pseudo-random numbers. Knuth couldn't have gone so far as to implement a quadratic congruential method, right? Knuth has nothing to do with the ISO C Standard library (unless he is on the ISO C Committee, which I think he isn't, BICBW), and he certainly has not implemented anything that can be called "the" Standard library. All the Standard requires is that - rand() returns a sequence of PRNs, between 0 to RAND_MAX inclusive; - no library function shall call rand() (as far as the user can detect); - RAND_MAX is at least 32767; - srand() can be used to initialise rand()'s seed. Nothing else is required. An example is given as to how rand() _could_ be implemented, but that's all it is: an example. Richard Nov 13 '05 #3

 P: n/a John Schutkeker writes: Does anybody know what pseudo-random number generator was used to implement the RAN() function in the C standard math library? Knuth couldn't have gone so far as to implement a quadratic congruential method, right? Is it yet another implementation of the much reviled RANDU? Hm... well, first of all, neither RAN() nor RANDU() are in the C standard math library. Second, Donald Knuth had nothing whatsoever to do with the creation of the standard library, though it's conceivable that he has written one of his own. I would be less surprised if you had meant something like "the math library Knuth wrote in C". I still don't know to what you would be referring, but I don't doubt that he has written math libraries in C. But none of this is really topical in a newsgroup which discusses only the ISO C programming language, now is it? -Micah Nov 13 '05 #4

 P: n/a Did Ritchie, or whoever wrote this routine, ever publish a justification for the constant "1103515245." I guess the whole trick to writing a good LCG is to find the best possible value of that constant. There's obviously nothing special about the built in seed, "12345." "David A" wrote in news:R5*****************@news-server.bigpond.net.au: unsigned long _Randseed = 1; int (rand)(void) { _Randseed = _Randseed * 1103515245 + 12345; return ((unsigned int)(_Randseed >> 16) & RAND_MAX); } Nov 13 '05 #5

### This discussion thread is closed

Replies have been disabled for this discussion.

### Similar topics

Browse more C / C++ Questions on Bytes