Mail Archives: djgpp/1998/03/30/10:02:41
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
- Raw text -