summaryrefslogtreecommitdiff
path: root/random.c
blob: 3997bcba26aa1843498eb4e41445c8de608dfedf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/* libnul - another utility library */

/* Based on grand.c from GLib. GLib copyright:
 *
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/* Originally developed and coded by Makoto Matsumoto and Takuji
 * Nishimura.  Please mail <matumoto@math.keio.ac.jp>, if you're using
 * code from this file in your own programs or libraries.
 * Further information on the Mersenne Twister can be found at
 * http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
 * This code was adapted to glib by Sebastian Wilhelmi.
 */

/*
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
 * file for a list of people on the GLib Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GLib at ftp://ftp.gtk.org/pub/gtk/.
 */

/*
 * MT safe
 */

#include "config.h"
#include "libnul.h"

#include <math.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

typedef struct _rand_t rand_t;

G_LOCK_DEFINE_STATIC (global_random);
static rand_t* global_random = NULL;

/* Period parameters */
#define N 624
#define M 397
#define MATRIX_A 0x9908b0df   /* constant vector a */
#define UPPER_MASK 0x80000000 /* most significant w-r bits */
#define LOWER_MASK 0x7fffffff /* least significant r bits */

/* Tempering parameters */
#define TEMPERING_MASK_B 0x9d2c5680
#define TEMPERING_MASK_C 0xefc60000
#define TEMPERING_SHIFT_U(y)  (y >> 11)
#define TEMPERING_SHIFT_S(y)  (y << 7)
#define TEMPERING_SHIFT_T(y)  (y << 15)
#define TEMPERING_SHIFT_L(y)  (y >> 18)

struct _rand_t
{
    uint32_t mt[N]; /* the array for the state vector  */
    guint mti;
};

/**
 * nul_rand_set_seed:
 * @rand_: a #rand_t.
 * @seed: a value to reinitialize the random number generator.
 *
 * Sets the seed for the random number generator #rand_t to @seed.
 **/
static void
nul_rand_set_seed (rand_t* rand, uint32_t seed)
{
    g_return_if_fail (rand != NULL);

    /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
    /* In the previous version (see above), MSBs of the    */
    /* seed affect only MSBs of the array mt[].            */

    rand->mt[0]= seed;
    for (rand->mti=1; rand->mti<N; rand->mti++)
    {
	rand->mt[rand->mti] = 1812433253UL *
	    (rand->mt[rand->mti-1] ^ (rand->mt[rand->mti-1] >> 30)) + rand->mti;
    }
}

/**
 * nul_rand_set_seed_array:
 * @rand_: a #rand_t.
 * @seed: array to initialize with
 * @seed_length: length of array
 *
 * Initializes the random number generator by an array of
 * longs.  Array can be of arbitrary size, though only the
 * first 624 values are taken.  This function is useful
 * if you have many low entropy seeds, or if you require more then
 * 32bits of actual entropy for your application.
 *
 * Since: 2.4
 **/
static void
nul_rand_set_seed_array (rand_t* rand, const uint32_t *seed, guint seed_length)
{
    int i, j, k;

    g_return_if_fail (rand != NULL);
    g_return_if_fail (seed_length >= 1);

    nul_rand_set_seed (rand, 19650218UL);

    i=1; j=0;
    k = (N>seed_length ? N : seed_length);
    for (; k; k--)
    {
	rand->mt[i] = (rand->mt[i] ^
		       ((rand->mt[i-1] ^ (rand->mt[i-1] >> 30)) * 1664525UL))
	    + seed[j] + j; /* non linear */
	rand->mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
	i++; j++;
	if (i>=N)
        {
	    rand->mt[0] = rand->mt[N-1];
	    i=1;
	}
	if (j>=seed_length)
	    j=0;
    }
    for (k=N-1; k; k--)
    {
	rand->mt[i] = (rand->mt[i] ^
		       ((rand->mt[i-1] ^ (rand->mt[i-1] >> 30)) * 1566083941UL))
	    - i; /* non linear */
	rand->mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
	i++;
	if (i>=N)
        {
	    rand->mt[0] = rand->mt[N-1];
	    i=1;
	}
    }

    rand->mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
}

/**
 * nul_rand_new_with_seed:
 * @seed: a value to initialize the random number generator.
 *
 * Creates a new random number generator initialized with @seed.
 *
 * Return value: the new #rand_t.
 **/
static rand_t*
nul_rand_new_with_seed (uint32_t seed)
{
    rand_t *rand = g_new0 (rand_t, 1);
    nul_rand_set_seed (rand, seed);
    return rand;
}

/**
 * nul_rand_new_with_seed_array:
 * @seed: an array of seeds to initialize the random number generator.
 * @seed_length: an array of seeds to initialize the random number generator.
 *
 * Creates a new random number generator initialized with @seed.
 *
 * Return value: the new #rand_t.
 *
 * Since: 2.4
 **/
static rand_t*
nul_rand_new_with_seed_array (const uint32_t *seed, guint seed_length)
{
    rand_t *rand = g_new0 (rand_t, 1);

    nul_rand_set_seed_array (rand, seed, seed_length);

    return rand;
}

/**
 * nul_rand_new:
 *
 * Creates a new random number generator initialized with a seed taken
 * either from <filename>/dev/urandom</filename> (if existing) or from
 * the current time (as a fallback).
 *
 * Return value: the new #rand_t.
 **/
static rand_t*
nul_rand_new (void)
{
    uint32_t seed[4];
    GTimeVal now;
#ifdef G_OS_UNIX
    static gboolean dev_urandom_exists = TRUE;

    if (dev_urandom_exists)
    {
	FILE* dev_urandom;

	do
        {
	    errno = 0;
	    dev_urandom = fopen("/dev/urandom", "rb");
	}
	while G_UNLIKELY (errno == EINTR);

	if (dev_urandom)
	{
	    int r;

	    do
	    {
		errno = 0;
		r = fread (seed, sizeof (seed), 1, dev_urandom);
	    }
	    while G_UNLIKELY (errno == EINTR);

	    if (r != 1)
		dev_urandom_exists = FALSE;

	    fclose (dev_urandom);
	}
	else
	    dev_urandom_exists = FALSE;
    }
#else
    static gboolean dev_urandom_exists = FALSE;
#endif

    if (!dev_urandom_exists)
    {
	g_get_current_time (&now);
	seed[0] = now.tv_sec;
	seed[1] = now.tv_usec;
	seed[2] = getpid ();
#ifdef G_OS_UNIX
	seed[3] = getppid ();
#else
	seed[3] = 0;
#endif
    }

    return nul_rand_new_with_seed_array (seed, 4);
}

/**
 * nul_rand_int:
 * @rand_: a #rand_t.
 *
 * Returns the next random #uint32_t from @rand_ equally distributed over
 * the range [0..2^32-1].
 *
 * Return value: A random number.
 **/
static uint32_t
nul_rand_int (rand_t* rand)
{
    uint32_t y;
    static const uint32_t mag01[2]={0x0, MATRIX_A};
    /* mag01[x] = x * MATRIX_A  for x=0,1 */

    g_return_val_if_fail (rand != NULL, 0);

    if (rand->mti >= N) { /* generate N words at one time */
	int kk;

	for (kk=0;kk<N-M;kk++) {
	    y = (rand->mt[kk]&UPPER_MASK)|(rand->mt[kk+1]&LOWER_MASK);
	    rand->mt[kk] = rand->mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1];
	}
	for (;kk<N-1;kk++) {
	    y = (rand->mt[kk]&UPPER_MASK)|(rand->mt[kk+1]&LOWER_MASK);
	    rand->mt[kk] = rand->mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1];
	}
	y = (rand->mt[N-1]&UPPER_MASK)|(rand->mt[0]&LOWER_MASK);
	rand->mt[N-1] = rand->mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1];

	rand->mti = 0;
    }

    y = rand->mt[rand->mti++];
    y ^= TEMPERING_SHIFT_U(y);
    y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
    y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
    y ^= TEMPERING_SHIFT_L(y);

    return y;
}

/* transform [0..2^32] -> [0..1] */
#define NUL_RAND_DOUBLE_TRANSFORM 2.3283064365386962890625e-10

/**
 * nul_rand_int_range:
 * @rand_: a #rand_t.
 * @begin: lower closed bound of the interval.
 * @end: upper open bound of the interval.
 *
 * Returns the next random #int32_t from @rand_ equally distributed over
 * the range [@begin..@end-1].
 *
 * Return value: A random number.
 **/
static int32_t
nul_rand_int_range (rand_t* rand, int32_t begin, int32_t end)
{
    uint32_t dist = end - begin;
    uint32_t random;

    g_return_val_if_fail (rand != NULL, begin);
    g_return_val_if_fail (end > begin, begin);

    if (dist == 0)
	random = 0;
    else
    {
	/* maxvalue is set to the predecessor of the greatest
	 * multiple of dist less or equal 2^32. */
	uint32_t maxvalue;
	if (dist <= 0x80000000u) /* 2^31 */
	{
	    /* maxvalue = 2^32 - 1 - (2^32 % dist) */
	    uint32_t leftover = (0x80000000u % dist) * 2;
	    if (leftover >= dist) leftover -= dist;
	    maxvalue = 0xffffffffu - leftover;
	}
	else
	    maxvalue = dist - 1;

	do
	    random = nul_rand_int (rand);
	while (random > maxvalue);

	random %= dist;
    }
    return begin + random;
}

/**
 * nul_rand_double:
 * @rand_: a #rand_t.
 *
 * Returns the next random #double from @rand_ equally distributed over
 * the range [0..1).
 *
 * Return value: A random number.
 **/
static double
nul_rand_double (rand_t* rand)
{
    /* We set all 52 bits after the point for this, not only the first
       32. Thats why we need two calls to nul_rand_int */
    double retval = nul_rand_int (rand) * NUL_RAND_DOUBLE_TRANSFORM;
    retval = (retval + nul_rand_int (rand)) * NUL_RAND_DOUBLE_TRANSFORM;

    /* The following might happen due to very bad rounding luck, but
     * actually this should be more than rare, we just try again then */
    if (retval >= 1.0)
	return nul_rand_double (rand);

    return retval;
}

/**
 * nul_rand_double_range:
 * @rand_: a #rand_t.
 * @begin: lower closed bound of the interval.
 * @end: upper open bound of the interval.
 *
 * Returns the next random #double from @rand_ equally distributed over
 * the range [@begin..@end).
 *
 * Return value: A random number.
 **/
static double
nul_rand_double_range (rand_t* rand, double begin, double end)
{
    return nul_rand_double (rand) * (end - begin) + begin;
}

/**
 * nul_random_int:
 *
 * Return a random #uint32_t equally distributed over the range
 * [0..2^32-1].
 *
 * Return value: A random number.
 **/
uint32_t
nul_random_int (void)
{
    uint32_t result;
    G_LOCK (global_random);
    if (!global_random)
	global_random = nul_rand_new ();

    result = nul_rand_int (global_random);
    G_UNLOCK (global_random);
    return result;
}

/**
 * nul_random_int_range:
 * @begin: lower closed bound of the interval.
 * @end: upper open bound of the interval.
 *
 * Returns a random #int32_t equally distributed over the range
 * [@begin..@end-1].
 *
 * Return value: A random number.
 **/
int32_t
nul_random_int_range (int32_t begin, int32_t end)
{
    int32_t result;
    G_LOCK (global_random);
    if (!global_random)
	global_random = nul_rand_new ();

    result = nul_rand_int_range (global_random, begin, end);
    G_UNLOCK (global_random);
    return result;
}

/**
 * nul_random_double:
 *
 * Returns a random #double equally distributed over the range [0..1).
 *
 * Return value: A random number.
 **/
double
nul_random_double (void)
{
    double result;
    G_LOCK (global_random);
    if (!global_random)
	global_random = nul_rand_new ();

    result = nul_rand_double (global_random);
    G_UNLOCK (global_random);
    return result;
}

/**
 * nul_random_double_range:
 * @begin: lower closed bound of the interval.
 * @end: upper open bound of the interval.
 *
 * Returns a random #double equally distributed over the range [@begin..@end).
 *
 * Return value: A random number.
 **/
double
nul_random_double_range (double begin, double end)
{
    double result;
    G_LOCK (global_random);
    if (!global_random)
	global_random = nul_rand_new ();

    result = nul_rand_double_range (global_random, begin, end);
    G_UNLOCK (global_random);
    return result;
}

/**
 * nul_random_set_seed:
 * @seed: a value to reinitialize the global random number generator.
 *
 * Sets the seed for the global random number generator, which is used
 * by the <function>nul_random_*</function> functions, to @seed.
 **/
void
nul_random_set_seed (uint32_t seed)
{
    G_LOCK (global_random);
    if (!global_random)
	global_random = nul_rand_new_with_seed (seed);
    else
	nul_rand_set_seed (global_random, seed);
    G_UNLOCK (global_random);
}


#define __NUL_RAND_C__