Re: Proposal: Addition of a random number generator to GLib

Owen Taylor wrote:
> Sebastian Wilhelmi <> writes:
> > 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).
> I thought the poor quality of the low bits was a artifact
> of the typical linear-congruential generator found on
> older systems. (I don't think that the standard rand()
> on glibc systems even is vulnerable to that, and the
> Mersenne twister certainly shouldn't be if it is as
> good as claimed...)

Ok, I mailed Makoto Matsumoto and asked about that: He said:

>result = MT_rand () % n;
>result = floor (MT_rand () * scale_to_0_to_1 * n);

I say the latter is better for small n, say, n < 2^16,
since MT is optimized with respect to the MSBs.

But of course I forgot to treat the case n > 2^16 special as problems will
arise then.(*) I have a solution for that on my computer however.


(*) Take n = 2^32-2. Then 0 will occur twice as often as any other number.
This holds true for both methods by the way.
Sebastian Wilhelmi                   |            här ovanför alla molnen           |      är himmlen så förunerligt blå   |

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