Proposal: Addition of a random number generator to GLib



Hi,

I propose to include a random number generator into GLib and I propose to
use Mersenne Twister, see http://www.math.keio.ac.jp/~matumoto/emt.html
I have attached a patch to include the MT into GLib.

Why include a random number generator:
Because it is good to have an random number generator with:
1) the same quality on all platforms.
2) good multithread behaviour (rand_r plainly sucks)
3) portable seed values.

Why use Mersenne Twister?
1) It has a high quality (check the website above)
2) It is fast
3) It is free of patents etc.
4) It has a LGPL implementation
5) The Authors already agreed, that we could use their code.
6) It is already used by other OS projetcs (Not that this is very
   convincing......), such as PHP3. 

Why use the sample code and not code it newly?
Because a random number generator is a very delicate thing and taking the
sample code with as few as possible changes is best, I think.

Why the g_random_... interface?
Because most people just need one random value and don't want to create an
object for it first. This has to be done MT-safe also.

Why then the g_rand_.. interface?
Some people need a reproducible sequence of random numbers and therefore
you need such a consruction.

Why provide a ..._int_range function?
Because often people do this by writing rand()%CONSTANT. This however in
general yields random numbers of bad quality. If we provide a function to
do that, we will improve the quality of random numbers. (on the cost of
speed though).

Why has the interval for int and double a closed border down and an open
border up?
This is often used, but I agree, that there might not be a best solution
here. For ints I would prefer this however, as it resembles the C spirit
of arrays. g_random_int_range (0, s) yields a valid index for an array
with the size 's'.

Bye,
Sebastian

-- 
Sebastian Wilhelmi                   |            här ovanför alla molnen
mailto:wilhelmi@ira.uka.de           |      är himmlen så förunerligt blå
http://goethe.ira.uka.de/~wilhelmi   |

grand.diff

grand.c



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]