Coverage Report

Created: 2025-11-16 06:06

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