Re: Suggestion: Change the specification for grand in glib
- From: Sebastian Wilhelmi <wilhelmi ira uka de>
- To: Sverre Johansen <sj ifi uio no>
- Cc: Gtk Development List <gtk-devel-list gnome org>
- Subject: Re: Suggestion: Change the specification for grand in glib
- Date: Tue, 19 Dec 2000 11:23:35 +0100
Hi Sverre,
> If the application for g_rand_double is g_rand_double_range or
> g_rand_int_range you are correct. But in simulations where we test on
> really low probabilities it is important that g_rand_double works near
> 0.0. But in that case we do not need all 53 bits of accuracy.
>
> If you whant to spare one test then the following could be used:
>
> gdouble
> g_rand_double (GRand* rand)
> {
> gdouble transform = G_RAND_DOUBLE_TRANSFORM;
> guint32 r1 = g_rand_int (rand);
> gdouble r2 = g_rand_int (rand);
>
> if (r1 < 1<<20)
> {
> while (r1 == 0)
> {
> transform *= G_RAND_DOUBLE_TRANSFORM;
> r1 = g_rand_int (rand);
> }
> r2 += g_rand_int (rand) * G_RAND_DOUBLE_TRANSFORM;
> }
>
> return (r2 * G_RAND_DOUBLE_TRANSFORM + r1) * transform;
> }
>
> The following version should be sufficient:
>
> gdouble
> g_rand_double (GRand* rand)
> {
> gdouble transform = G_RAND_DOUBLE_TRANSFORM;
> guint32 r1 = g_rand_int (rand);
>
> while (r1 == 0)
> {
> transform *= G_RAND_DOUBLE_TRANSFORM;
> r1 = g_rand_int (rand);
> }
>
> return (g_rand_int (rand) * G_RAND_DOUBLE_TRANSFORM + r1) * transform;
> }
>
> Or even simpler without the loop:
>
> gdouble
> g_rand_double (GRand* rand)
> {
> guint32 r1 = g_rand_int (rand);
>
> if (r1 == 0)
> return (g_rand_int (rand) * G_RAND_DOUBLE_TRANSFORM + g_rand_int (rand))
> * (G_RAND_DOUBLE_TRANSFORM * G_RAND_DOUBLE_TRANSFORM)
> else
> return (g_rand_int (rand) * G_RAND_DOUBLE_TRANSFORM + r1)
> * G_RAND_DOUBLE_TRANSFORM;
> }
I'm sorry, but I'm not going to include that. It happens in .00000002% of all
cases and is IMHO of dubious usefulness.
God Jul og Hej då,
Sebastian
--
Sebastian Wilhelmi
mailto:wilhelmi ira uka de
http://goethe.ira.uka.de/~wilhelmi
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]