Message-ID: <351F9F19.693F@pobox.oleane.com> Date: Mon, 30 Mar 1998 15:33:13 +0200 From: Francois Charton Organization: CCMSA MIME-Version: 1.0 To: Paul Derbyshire CC: djgpp AT delorie DOT com Subject: Re: Srandom() References: <6fku51$909 AT freenet-news DOT carleton DOT ca> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Precedence: bulk Paul Derbyshire wrote: > > How quickly does srandom() in DJGPP's libc execute? > srand(x) is as fast as can be: it affects the value x to some global variable. For srandom(x), your mileage may vary: there are actually five "flavours" of random(x), #defined as TYPE_0 to TYPE_4, the higher the type, the randomer the function (the default type used in libc is 3, to change it, I think you need to recompile the library). Type 0 random() is a standard linear congruential generator, srandom(x) is the same as srand(). For type N, srandom() 1/ initialise N states by calling a rand()-like RNG, N times 2/ avoids "short term effects" by calling random() 10*N times in a row (this is the longer part of the program).. As a result, type N srandom() takes about 10*N times the execution time for random() to execute. For the default type 3 random(), srandom() calls random() 30 times to initialise. > Would an added > srandom() before every random() in a program that uses random() > extensively cause a noticeable slowdown? If it is before every single call to random(), the answer is yes, as soon as random() represents a noticeable part of the execution time of your program. Anyway, this is clearly a bad idea: the randomness of random() is no more used. (if you did that with rand(), calling srand(time()) before every occurence of rand(), this would amount to using time() as a RNG) > Reason I ask is because something > like that might be necessary to make a threadsafe random number generator > class so that each random number generator instance can be seeded > independently and give consistent results. > If you want "replicability" of your random series is a multitasking environment, each program has to have its own random series. If each instance of random() uses long rnadom series (ie several hundred), the srandom() overhead will not be too high. However, if you use such a device, be careful that no program uses random numbers from two different series: rand() or random() can guarantee that all numbers in a series have certain random properties. It is not clear, though, that the numbers from two "parallel" series will be uncorrelated (this is wrong in some cases for Linear congruential generators at least). Regards Francois