Coverage Report

Created: 2025-06-22 06:29

/src/glib/glib/grand.c
Line
Count
Source (jump to first uncovered line)
1
/* GLIB - Library of useful routines for C programming
2
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3
 *
4
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
8
 *
9
 * This library is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
/* Originally developed and coded by Makoto Matsumoto and Takuji
19
 * Nishimura.  Please mail <matumoto@math.keio.ac.jp>, if you're using
20
 * code from this file in your own programs or libraries.
21
 * Further information on the Mersenne Twister can be found at
22
 * http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
23
 * This code was adapted to glib by Sebastian Wilhelmi.
24
 */
25
26
/*
27
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
28
 * file for a list of people on the GLib Team.  See the ChangeLog
29
 * files for a list of changes.  These files are distributed with
30
 * GLib at ftp://ftp.gtk.org/pub/gtk/.
31
 */
32
33
/*
34
 * MT safe
35
 */
36
37
#include "config.h"
38
#define _CRT_RAND_S
39
40
#include <math.h>
41
#include <errno.h>
42
#include <stdio.h>
43
#include <string.h>
44
#include <sys/types.h>
45
#include "grand.h"
46
47
#include "genviron.h"
48
#include "gmain.h"
49
#include "gmem.h"
50
#include "gtestutils.h"
51
#include "gthread.h"
52
#include "gtimer.h"
53
54
#ifdef G_OS_UNIX
55
#include <unistd.h>
56
#endif
57
58
#ifdef G_OS_WIN32
59
#include <stdlib.h>
60
#include <process.h> /* For getpid() */
61
#endif
62
63
/**
64
 * SECTION:random_numbers
65
 * @title: Random Numbers
66
 * @short_description: pseudo-random number generator
67
 *
68
 * The following functions allow you to use a portable, fast and good
69
 * pseudo-random number generator (PRNG).
70
 * 
71
 * Do not use this API for cryptographic purposes such as key
72
 * generation, nonces, salts or one-time pads.
73
 *
74
 * This PRNG is suitable for non-cryptographic use such as in games
75
 * (shuffling a card deck, generating levels), generating data for
76
 * a test suite, etc. If you need random data for cryptographic
77
 * purposes, it is recommended to use platform-specific APIs such
78
 * as `/dev/random` on UNIX, or CryptGenRandom() on Windows.
79
 *
80
 * GRand uses the Mersenne Twister PRNG, which was originally
81
 * developed by Makoto Matsumoto and Takuji Nishimura. Further
82
 * information can be found at
83
 * [this page](http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html).
84
 *
85
 * If you just need a random number, you simply call the g_random_*
86
 * functions, which will create a globally used #GRand and use the
87
 * according g_rand_* functions internally. Whenever you need a
88
 * stream of reproducible random numbers, you better create a
89
 * #GRand yourself and use the g_rand_* functions directly, which
90
 * will also be slightly faster. Initializing a #GRand with a
91
 * certain seed will produce exactly the same series of random
92
 * numbers on all platforms. This can thus be used as a seed for
93
 * e.g. games.
94
 *
95
 * The g_rand*_range functions will return high quality equally
96
 * distributed random numbers, whereas for example the
97
 * `(g_random_int()%max)` approach often
98
 * doesn't yield equally distributed numbers.
99
 *
100
 * GLib changed the seeding algorithm for the pseudo-random number
101
 * generator Mersenne Twister, as used by #GRand. This was necessary,
102
 * because some seeds would yield very bad pseudo-random streams.
103
 * Also the pseudo-random integers generated by g_rand*_int_range()
104
 * will have a slightly better equal distribution with the new
105
 * version of GLib.
106
 *
107
 * The original seeding and generation algorithms, as found in
108
 * GLib 2.0.x, can be used instead of the new ones by setting the
109
 * environment variable `G_RANDOM_VERSION` to the value of '2.0'.
110
 * Use the GLib-2.0 algorithms only if you have sequences of numbers
111
 * generated with Glib-2.0 that you need to reproduce exactly.
112
 */
113
114
/**
115
 * GRand:
116
 *
117
 * The GRand struct is an opaque data structure. It should only be
118
 * accessed through the g_rand_* functions.
119
 **/
120
121
G_LOCK_DEFINE_STATIC (global_random);
122
123
/* Period parameters */  
124
0
#define N 624
125
0
#define M 397
126
0
#define MATRIX_A 0x9908b0df   /* constant vector a */
127
0
#define UPPER_MASK 0x80000000 /* most significant w-r bits */
128
0
#define LOWER_MASK 0x7fffffff /* least significant r bits */
129
130
/* Tempering parameters */   
131
0
#define TEMPERING_MASK_B 0x9d2c5680
132
0
#define TEMPERING_MASK_C 0xefc60000
133
0
#define TEMPERING_SHIFT_U(y)  (y >> 11)
134
0
#define TEMPERING_SHIFT_S(y)  (y << 7)
135
0
#define TEMPERING_SHIFT_T(y)  (y << 15)
136
0
#define TEMPERING_SHIFT_L(y)  (y >> 18)
137
138
static guint
139
get_random_version (void)
140
0
{
141
0
  static gsize initialized = FALSE;
142
0
  static guint random_version;
143
144
0
  if (g_once_init_enter (&initialized))
145
0
    {
146
0
      const gchar *version_string = g_getenv ("G_RANDOM_VERSION");
147
0
      if (!version_string || version_string[0] == '\000' || 
148
0
    strcmp (version_string, "2.2") == 0)
149
0
  random_version = 22;
150
0
      else if (strcmp (version_string, "2.0") == 0)
151
0
  random_version = 20;
152
0
      else
153
0
  {
154
0
    g_warning ("Unknown G_RANDOM_VERSION \"%s\". Using version 2.2.",
155
0
         version_string);
156
0
    random_version = 22;
157
0
  }
158
0
      g_once_init_leave (&initialized, TRUE);
159
0
    }
160
  
161
0
  return random_version;
162
0
}
163
164
struct _GRand
165
{
166
  guint32 mt[N]; /* the array for the state vector  */
167
  guint mti; 
168
};
169
170
/**
171
 * g_rand_new_with_seed:
172
 * @seed: a value to initialize the random number generator
173
 * 
174
 * Creates a new random number generator initialized with @seed.
175
 * 
176
 * Returns: the new #GRand
177
 **/
178
GRand*
179
g_rand_new_with_seed (guint32 seed)
180
0
{
181
0
  GRand *rand = g_new0 (GRand, 1);
182
0
  g_rand_set_seed (rand, seed);
183
0
  return rand;
184
0
}
185
186
/**
187
 * g_rand_new_with_seed_array:
188
 * @seed: an array of seeds to initialize the random number generator
189
 * @seed_length: an array of seeds to initialize the random number
190
 *     generator
191
 * 
192
 * Creates a new random number generator initialized with @seed.
193
 * 
194
 * Returns: the new #GRand
195
 *
196
 * Since: 2.4
197
 */
198
GRand*
199
g_rand_new_with_seed_array (const guint32 *seed,
200
                            guint          seed_length)
201
0
{
202
0
  GRand *rand = g_new0 (GRand, 1);
203
0
  g_rand_set_seed_array (rand, seed, seed_length);
204
0
  return rand;
205
0
}
206
207
/**
208
 * g_rand_new:
209
 * 
210
 * Creates a new random number generator initialized with a seed taken
211
 * either from `/dev/urandom` (if existing) or from the current time
212
 * (as a fallback).
213
 *
214
 * On Windows, the seed is taken from rand_s().
215
 * 
216
 * Returns: the new #GRand
217
 */
218
GRand* 
219
g_rand_new (void)
220
0
{
221
0
  guint32 seed[4];
222
0
#ifdef G_OS_UNIX
223
0
  static gboolean dev_urandom_exists = TRUE;
224
225
0
  if (dev_urandom_exists)
226
0
    {
227
0
      FILE* dev_urandom;
228
229
0
      do
230
0
  {
231
0
    dev_urandom = fopen("/dev/urandom", "rb");
232
0
  }
233
0
      while G_UNLIKELY (dev_urandom == NULL && errno == EINTR);
234
235
0
      if (dev_urandom)
236
0
  {
237
0
    int r;
238
239
0
    setvbuf (dev_urandom, NULL, _IONBF, 0);
240
0
    do
241
0
      {
242
0
        errno = 0;
243
0
        r = fread (seed, sizeof (seed), 1, dev_urandom);
244
0
      }
245
0
    while G_UNLIKELY (errno == EINTR);
246
247
0
    if (r != 1)
248
0
      dev_urandom_exists = FALSE;
249
250
0
    fclose (dev_urandom);
251
0
  } 
252
0
      else
253
0
  dev_urandom_exists = FALSE;
254
0
    }
255
256
0
  if (!dev_urandom_exists)
257
0
    {
258
0
      gint64 now_us = g_get_real_time ();
259
0
      seed[0] = now_us / G_USEC_PER_SEC;
260
0
      seed[1] = now_us % G_USEC_PER_SEC;
261
0
      seed[2] = getpid ();
262
0
      seed[3] = getppid ();
263
0
    }
264
#else /* G_OS_WIN32 */
265
  /* rand_s() is only available since Visual Studio 2005 and
266
   * MinGW-w64 has a wrapper that will emulate rand_s() if it's not in msvcrt
267
   */
268
#if (defined(_MSC_VER) && _MSC_VER >= 1400) || defined(__MINGW64_VERSION_MAJOR)
269
  gint i;
270
271
  for (i = 0; i < G_N_ELEMENTS (seed); i++)
272
    rand_s (&seed[i]);
273
#else
274
#warning Using insecure seed for random number generation because of missing rand_s() in Windows XP
275
  GTimeVal now;
276
277
  g_get_current_time (&now);
278
  seed[0] = now.tv_sec;
279
  seed[1] = now.tv_usec;
280
  seed[2] = getpid ();
281
  seed[3] = 0;
282
#endif
283
284
#endif
285
286
0
  return g_rand_new_with_seed_array (seed, 4);
287
0
}
288
289
/**
290
 * g_rand_free:
291
 * @rand_: a #GRand
292
 *
293
 * Frees the memory allocated for the #GRand.
294
 */
295
void
296
g_rand_free (GRand *rand)
297
0
{
298
0
  g_return_if_fail (rand != NULL);
299
300
0
  g_free (rand);
301
0
}
302
303
/**
304
 * g_rand_copy:
305
 * @rand_: a #GRand
306
 *
307
 * Copies a #GRand into a new one with the same exact state as before.
308
 * This way you can take a snapshot of the random number generator for
309
 * replaying later.
310
 *
311
 * Returns: the new #GRand
312
 *
313
 * Since: 2.4
314
 */
315
GRand*
316
g_rand_copy (GRand *rand)
317
0
{
318
0
  GRand* new_rand;
319
320
0
  g_return_val_if_fail (rand != NULL, NULL);
321
322
0
  new_rand = g_new0 (GRand, 1);
323
0
  memcpy (new_rand, rand, sizeof (GRand));
324
325
0
  return new_rand;
326
0
}
327
328
/**
329
 * g_rand_set_seed:
330
 * @rand_: a #GRand
331
 * @seed: a value to reinitialize the random number generator
332
 *
333
 * Sets the seed for the random number generator #GRand to @seed.
334
 */
335
void
336
g_rand_set_seed (GRand   *rand,
337
                 guint32  seed)
338
0
{
339
0
  g_return_if_fail (rand != NULL);
340
341
0
  switch (get_random_version ())
342
0
    {
343
0
    case 20:
344
      /* setting initial seeds to mt[N] using         */
345
      /* the generator Line 25 of Table 1 in          */
346
      /* [KNUTH 1981, The Art of Computer Programming */
347
      /*    Vol. 2 (2nd Ed.), pp102]                  */
348
      
349
0
      if (seed == 0) /* This would make the PRNG produce only zeros */
350
0
  seed = 0x6b842128; /* Just set it to another number */
351
      
352
0
      rand->mt[0]= seed;
353
0
      for (rand->mti=1; rand->mti<N; rand->mti++)
354
0
  rand->mt[rand->mti] = (69069 * rand->mt[rand->mti-1]);
355
      
356
0
      break;
357
0
    case 22:
358
      /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
359
      /* In the previous version (see above), MSBs of the    */
360
      /* seed affect only MSBs of the array mt[].            */
361
      
362
0
      rand->mt[0]= seed;
363
0
      for (rand->mti=1; rand->mti<N; rand->mti++)
364
0
  rand->mt[rand->mti] = 1812433253UL * 
365
0
    (rand->mt[rand->mti-1] ^ (rand->mt[rand->mti-1] >> 30)) + rand->mti; 
366
0
      break;
367
0
    default:
368
0
      g_assert_not_reached ();
369
0
    }
370
0
}
371
372
/**
373
 * g_rand_set_seed_array:
374
 * @rand_: a #GRand
375
 * @seed: array to initialize with
376
 * @seed_length: length of array
377
 *
378
 * Initializes the random number generator by an array of longs.
379
 * Array can be of arbitrary size, though only the first 624 values
380
 * are taken.  This function is useful if you have many low entropy
381
 * seeds, or if you require more then 32 bits of actual entropy for
382
 * your application.
383
 *
384
 * Since: 2.4
385
 */
386
void
387
g_rand_set_seed_array (GRand         *rand,
388
                       const guint32 *seed,
389
                       guint          seed_length)
390
0
{
391
0
  guint i, j, k;
392
393
0
  g_return_if_fail (rand != NULL);
394
0
  g_return_if_fail (seed_length >= 1);
395
396
0
  g_rand_set_seed (rand, 19650218UL);
397
398
0
  i=1; j=0;
399
0
  k = (N>seed_length ? N : seed_length);
400
0
  for (; k; k--)
401
0
    {
402
0
      rand->mt[i] = (rand->mt[i] ^
403
0
         ((rand->mt[i-1] ^ (rand->mt[i-1] >> 30)) * 1664525UL))
404
0
        + seed[j] + j; /* non linear */
405
0
      rand->mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
406
0
      i++; j++;
407
0
      if (i>=N)
408
0
        {
409
0
    rand->mt[0] = rand->mt[N-1];
410
0
    i=1;
411
0
  }
412
0
      if (j>=seed_length)
413
0
  j=0;
414
0
    }
415
0
  for (k=N-1; k; k--)
416
0
    {
417
0
      rand->mt[i] = (rand->mt[i] ^
418
0
         ((rand->mt[i-1] ^ (rand->mt[i-1] >> 30)) * 1566083941UL))
419
0
        - i; /* non linear */
420
0
      rand->mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
421
0
      i++;
422
0
      if (i>=N)
423
0
        {
424
0
    rand->mt[0] = rand->mt[N-1];
425
0
    i=1;
426
0
  }
427
0
    }
428
429
0
  rand->mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ 
430
0
}
431
432
/**
433
 * g_rand_boolean:
434
 * @rand_: a #GRand
435
 *
436
 * Returns a random #gboolean from @rand_.
437
 * This corresponds to an unbiased coin toss.
438
 *
439
 * Returns: a random #gboolean
440
 */
441
/**
442
 * g_rand_int:
443
 * @rand_: a #GRand
444
 *
445
 * Returns the next random #guint32 from @rand_ equally distributed over
446
 * the range [0..2^32-1].
447
 *
448
 * Returns: a random number
449
 */
450
guint32
451
g_rand_int (GRand *rand)
452
0
{
453
0
  guint32 y;
454
0
  static const guint32 mag01[2]={0x0, MATRIX_A};
455
  /* mag01[x] = x * MATRIX_A  for x=0,1 */
456
457
0
  g_return_val_if_fail (rand != NULL, 0);
458
459
0
  if (rand->mti >= N) { /* generate N words at one time */
460
0
    int kk;
461
    
462
0
    for (kk = 0; kk < N - M; kk++) {
463
0
      y = (rand->mt[kk]&UPPER_MASK)|(rand->mt[kk+1]&LOWER_MASK);
464
0
      rand->mt[kk] = rand->mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1];
465
0
    }
466
0
    for (; kk < N - 1; kk++) {
467
0
      y = (rand->mt[kk]&UPPER_MASK)|(rand->mt[kk+1]&LOWER_MASK);
468
0
      rand->mt[kk] = rand->mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1];
469
0
    }
470
0
    y = (rand->mt[N-1]&UPPER_MASK)|(rand->mt[0]&LOWER_MASK);
471
0
    rand->mt[N-1] = rand->mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1];
472
    
473
0
    rand->mti = 0;
474
0
  }
475
  
476
0
  y = rand->mt[rand->mti++];
477
0
  y ^= TEMPERING_SHIFT_U(y);
478
0
  y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
479
0
  y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
480
0
  y ^= TEMPERING_SHIFT_L(y);
481
  
482
0
  return y; 
483
0
}
484
485
/* transform [0..2^32] -> [0..1] */
486
0
#define G_RAND_DOUBLE_TRANSFORM 2.3283064365386962890625e-10
487
488
/**
489
 * g_rand_int_range:
490
 * @rand_: a #GRand
491
 * @begin: lower closed bound of the interval
492
 * @end: upper open bound of the interval
493
 *
494
 * Returns the next random #gint32 from @rand_ equally distributed over
495
 * the range [@begin..@end-1].
496
 *
497
 * Returns: a random number
498
 */
499
gint32 
500
g_rand_int_range (GRand  *rand,
501
                  gint32  begin,
502
                  gint32  end)
503
0
{
504
0
  guint32 dist = end - begin;
505
0
  guint32 random = 0;
506
507
0
  g_return_val_if_fail (rand != NULL, begin);
508
0
  g_return_val_if_fail (end > begin, begin);
509
510
0
  switch (get_random_version ())
511
0
    {
512
0
    case 20:
513
0
      if (dist <= 0x10000L) /* 2^16 */
514
0
  {
515
    /* This method, which only calls g_rand_int once is only good
516
     * for (end - begin) <= 2^16, because we only have 32 bits set
517
     * from the one call to g_rand_int ().
518
     *
519
     * We are using (trans + trans * trans), because g_rand_int only
520
     * covers [0..2^32-1] and thus g_rand_int * trans only covers
521
     * [0..1-2^-32], but the biggest double < 1 is 1-2^-52. 
522
     */
523
    
524
0
    gdouble double_rand = g_rand_int (rand) * 
525
0
      (G_RAND_DOUBLE_TRANSFORM +
526
0
       G_RAND_DOUBLE_TRANSFORM * G_RAND_DOUBLE_TRANSFORM);
527
    
528
0
    random = (gint32) (double_rand * dist);
529
0
  }
530
0
      else
531
0
  {
532
    /* Now we use g_rand_double_range (), which will set 52 bits
533
     * for us, so that it is safe to round and still get a decent
534
     * distribution
535
           */
536
0
    random = (gint32) g_rand_double_range (rand, 0, dist);
537
0
  }
538
0
      break;
539
0
    case 22:
540
0
      if (dist == 0)
541
0
  random = 0;
542
0
      else 
543
0
  {
544
    /* maxvalue is set to the predecessor of the greatest
545
     * multiple of dist less or equal 2^32.
546
     */
547
0
    guint32 maxvalue;
548
0
    if (dist <= 0x80000000u) /* 2^31 */
549
0
      {
550
        /* maxvalue = 2^32 - 1 - (2^32 % dist) */
551
0
        guint32 leftover = (0x80000000u % dist) * 2;
552
0
        if (leftover >= dist) leftover -= dist;
553
0
        maxvalue = 0xffffffffu - leftover;
554
0
      }
555
0
    else
556
0
      maxvalue = dist - 1;
557
    
558
0
    do
559
0
      random = g_rand_int (rand);
560
0
    while (random > maxvalue);
561
    
562
0
    random %= dist;
563
0
  }
564
0
      break;
565
0
    default:
566
0
      g_assert_not_reached ();
567
0
    }      
568
 
569
0
  return begin + random;
570
0
}
571
572
/**
573
 * g_rand_double:
574
 * @rand_: a #GRand
575
 *
576
 * Returns the next random #gdouble from @rand_ equally distributed over
577
 * the range [0..1).
578
 *
579
 * Returns: a random number
580
 */
581
gdouble 
582
g_rand_double (GRand *rand)
583
0
{    
584
  /* We set all 52 bits after the point for this, not only the first
585
     32. That's why we need two calls to g_rand_int */
586
0
  gdouble retval = g_rand_int (rand) * G_RAND_DOUBLE_TRANSFORM;
587
0
  retval = (retval + g_rand_int (rand)) * G_RAND_DOUBLE_TRANSFORM;
588
589
  /* The following might happen due to very bad rounding luck, but
590
   * actually this should be more than rare, we just try again then */
591
0
  if (retval >= 1.0) 
592
0
    return g_rand_double (rand);
593
594
0
  return retval;
595
0
}
596
597
/**
598
 * g_rand_double_range:
599
 * @rand_: a #GRand
600
 * @begin: lower closed bound of the interval
601
 * @end: upper open bound of the interval
602
 *
603
 * Returns the next random #gdouble from @rand_ equally distributed over
604
 * the range [@begin..@end).
605
 *
606
 * Returns: a random number
607
 */
608
gdouble 
609
g_rand_double_range (GRand   *rand,
610
                     gdouble  begin,
611
                     gdouble  end)
612
0
{
613
0
  gdouble r;
614
615
0
  r = g_rand_double (rand);
616
617
0
  return r * end - (r - 1) * begin;
618
0
}
619
620
static GRand *
621
get_global_random (void)
622
0
{
623
0
  static GRand *global_random;
624
625
  /* called while locked */
626
0
  if (!global_random)
627
0
    global_random = g_rand_new ();
628
629
0
  return global_random;
630
0
}
631
632
/**
633
 * g_random_boolean:
634
 *
635
 * Returns a random #gboolean.
636
 * This corresponds to an unbiased coin toss.
637
 *
638
 * Returns: a random #gboolean
639
 */
640
/**
641
 * g_random_int:
642
 *
643
 * Return a random #guint32 equally distributed over the range
644
 * [0..2^32-1].
645
 *
646
 * Returns: a random number
647
 */
648
guint32
649
g_random_int (void)
650
0
{
651
0
  guint32 result;
652
0
  G_LOCK (global_random);
653
0
  result = g_rand_int (get_global_random ());
654
0
  G_UNLOCK (global_random);
655
0
  return result;
656
0
}
657
658
/**
659
 * g_random_int_range:
660
 * @begin: lower closed bound of the interval
661
 * @end: upper open bound of the interval
662
 *
663
 * Returns a random #gint32 equally distributed over the range
664
 * [@begin..@end-1].
665
 *
666
 * Returns: a random number
667
 */
668
gint32 
669
g_random_int_range (gint32 begin,
670
                    gint32 end)
671
0
{
672
0
  gint32 result;
673
0
  G_LOCK (global_random);
674
0
  result = g_rand_int_range (get_global_random (), begin, end);
675
0
  G_UNLOCK (global_random);
676
0
  return result;
677
0
}
678
679
/**
680
 * g_random_double:
681
 *
682
 * Returns a random #gdouble equally distributed over the range [0..1).
683
 *
684
 * Returns: a random number
685
 */
686
gdouble 
687
g_random_double (void)
688
0
{
689
0
  double result;
690
0
  G_LOCK (global_random);
691
0
  result = g_rand_double (get_global_random ());
692
0
  G_UNLOCK (global_random);
693
0
  return result;
694
0
}
695
696
/**
697
 * g_random_double_range:
698
 * @begin: lower closed bound of the interval
699
 * @end: upper open bound of the interval
700
 *
701
 * Returns a random #gdouble equally distributed over the range
702
 * [@begin..@end).
703
 *
704
 * Returns: a random number
705
 */
706
gdouble 
707
g_random_double_range (gdouble begin,
708
                       gdouble end)
709
0
{
710
0
  double result;
711
0
  G_LOCK (global_random);
712
0
  result = g_rand_double_range (get_global_random (), begin, end);
713
0
  G_UNLOCK (global_random);
714
0
  return result;
715
0
}
716
717
/**
718
 * g_random_set_seed:
719
 * @seed: a value to reinitialize the global random number generator
720
 * 
721
 * Sets the seed for the global random number generator, which is used
722
 * by the g_random_* functions, to @seed.
723
 */
724
void
725
g_random_set_seed (guint32 seed)
726
0
{
727
0
  G_LOCK (global_random);
728
0
  g_rand_set_seed (get_global_random (), seed);
729
0
  G_UNLOCK (global_random);
730
0
}