Coverage Report

Created: 2025-07-01 07:00

/src/igraph/src/random/random.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
   IGraph library.
3
   Copyright (C) 2005-2012  Gabor Csardi <csardi.gabor@gmail.com>
4
   334 Harvard street, Cambridge, MA 02139 USA
5
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License as published by
8
   the Free Software Foundation; either version 2 of the License, or
9
   (at your option) any later version.
10
11
   This program 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
14
   GNU General Public License for more details.
15
16
   You should have received a copy of the GNU General Public License
17
   along with this program; if not, write to the Free Software
18
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
   02110-1301 USA
20
21
*/
22
23
24
#include "igraph_random.h"
25
26
#include "igraph_nongraph.h"
27
#include "igraph_error.h"
28
#include "igraph_types.h"
29
#include "igraph_vector.h"
30
31
#include "core/interruption.h"
32
#include "math/safe_intop.h"
33
#include "random/random_internal.h"
34
35
#include "config.h" /* IGRAPH_THREAD_LOCAL, HAVE___UINT128_T, HAVE__UMUL128 */
36
37
#if defined(HAVE__UMUL128) || defined(HAVE___UMULH)
38
#include <intrin.h> /* _umul128() or __umulh() are defined in intrin.h */
39
#endif
40
41
#include <assert.h>
42
#include <math.h>
43
#include <float.h> /* DBL_MANT_DIG */
44
#include <stdbool.h>
45
46
/**
47
 * \section about_rngs
48
 *
49
 * <section id="about-random-numbers-in-igraph">
50
 * <title>About random numbers in igraph</title>
51
 *
52
 * <para>
53
 * Some algorithms in igraph, such as sampling from random graph models,
54
 * require random number generators (RNGs). igraph includes a flexible
55
 * RNG framework that allows hooking up arbitrary random number generators,
56
 * and comes with several ready-to-use generators. This framework is used
57
 * in igraph's high-level interfaces to integrate with the host language's
58
 * own RNG.
59
 * </para>
60
 * </section>
61
 *
62
 */
63
64
/**
65
 * \section rng_use_cases
66
 *
67
 * <section id="random-use-cases"><title>Use cases</title>
68
 *
69
 * <section id="random-normal-use"><title>Normal (default) use</title>
70
 * <para>
71
 * If the user does not use any of the RNG functions explicitly, but calls
72
 * some of the randomized igraph functions, then a default RNG is set
73
 * up the first time an igraph function needs random numbers. The
74
 * seed of this RNG is the output of the <code>time(0)</code> function
75
 * call, using the <code>time</code> function from the standard C
76
 * library. This ensures that igraph creates a different random graph,
77
 * each time the C program is called.
78
 * </para>
79
 *
80
 * <para>
81
 * The created default generator is stored internally and can be
82
 * queried with the \ref igraph_rng_default() function.
83
 * </para>
84
 * </section>
85
 *
86
 * <section id="random-reproducible-simulations"><title>Reproducible simulations</title>
87
 * <para>
88
 * If reproducible results are needed, then the user should set the
89
 * seed of the default random number generator explicitly, using the
90
 * \ref igraph_rng_seed() function on the default generator, \ref
91
 * igraph_rng_default(). When setting the seed to the same number,
92
 * igraph generates exactly the same random graph (or series of random
93
 * graphs).
94
 * </para>
95
 * </section>
96
 *
97
 * <section id="random-changing-default-generator"><title>Changing the default generator</title>
98
 * <para>
99
 * By default igraph uses the \ref igraph_rng_default() random number
100
 * generator. This can be changed any time by calling \ref
101
 * igraph_rng_set_default(), with an already initialized random number
102
 * generator. Note that the old (replaced) generator is not
103
 * destroyed, so no memory is deallocated.
104
 * </para>
105
 * </section>
106
 *
107
 * <section id="random-using-multiple-generators"><title>Using multiple generators</title>
108
 * <para>
109
 * igraph also provides functions to set up multiple random number
110
 * generators, using the \ref igraph_rng_init() function, and then
111
 * generating random numbers from them, e.g. with \ref igraph_rng_get_integer()
112
 * and/or \ref igraph_rng_get_unif() calls.
113
 * </para>
114
 *
115
 * <para>
116
 * Note that initializing a new random number generator is
117
 * independent of the generator that the igraph functions themselves
118
 * use. If you want to replace that, then please use \ref
119
 * igraph_rng_set_default().
120
 * </para>
121
 * </section>
122
 *
123
 * <section id="random-example"><title>Example</title>
124
 * <para>
125
 * \example examples/simple/random_seed.c
126
 * </para>
127
 * </section>
128
 *
129
 * </section>
130
 */
131
132
/* ------------------------------------ */
133
134
/**
135
 * \var igraph_i_rng_default
136
 * The default igraph random number generator
137
 *
138
 * This generator is used by all builtin igraph functions that need to
139
 * generate random numbers; e.g. all random graph generators.
140
 *
141
 * You can use \ref igraph_i_rng_default with \ref igraph_rng_seed()
142
 * to set its seed.
143
 *
144
 * You can change the default generator using the \ref
145
 * igraph_rng_set_default() function.
146
 */
147
148
extern igraph_rng_t igraph_i_rng_default; /* defined in rng_pcg32.c */
149
IGRAPH_THREAD_LOCAL igraph_rng_t *igraph_i_rng_default_ptr = &igraph_i_rng_default;
150
151
/**
152
 * \function igraph_rng_set_default
153
 * \brief Set the default igraph random number generator.
154
 *
155
 * This function updates the default RNG used by igraph to be the one
156
 * pointed to by \p rng, and returns a pointer to the previous default
157
 * RNG. Future calls to \ref igraph_rng_default() will return the same
158
 * pointer as \p rng. The RNG pointed to by \p rng must not be destroyed
159
 * for as long as it is used as the default.
160
 *
161
 * \param rng The random number generator to use as default from now
162
 *    on. Calling \ref igraph_rng_destroy() on it, while it is still
163
 *    being used as the default will result in crashes and/or
164
 *    unpredictable results.
165
 * \return Pointer the previous default RNG.
166
 *
167
 * Time complexity: O(1).
168
 */
169
170
0
igraph_rng_t *igraph_rng_set_default(igraph_rng_t *rng) {
171
0
    igraph_rng_t *old_rng = igraph_i_rng_default_ptr;
172
0
    igraph_i_rng_default_ptr = rng;
173
0
    return old_rng;
174
0
}
175
176
177
/* ------------------------------------ */
178
179
/**
180
 * \function igraph_rng_default
181
 * \brief Query the default random number generator.
182
 *
183
 * \return A pointer to the default random number generator.
184
 *
185
 * \sa \ref igraph_rng_set_default()
186
 */
187
188
0
igraph_rng_t *igraph_rng_default(void) {
189
0
    return igraph_i_rng_default_ptr;
190
0
}
191
192
/* ------------------------------------ */
193
194
static igraph_uint_t igraph_i_rng_get_random_bits(igraph_rng_t *rng, uint8_t bits);
195
static uint64_t igraph_i_rng_get_random_bits_uint64(igraph_rng_t *rng, uint8_t bits);
196
197
static igraph_uint_t igraph_i_rng_get_uint(igraph_rng_t *rng);
198
static igraph_uint_t igraph_i_rng_get_uint_bounded(igraph_rng_t *rng, igraph_uint_t range);
199
200
static uint32_t igraph_i_rng_get_uint32(igraph_rng_t *rng);
201
static uint32_t igraph_i_rng_get_uint32_bounded(igraph_rng_t *rng, uint32_t range);
202
203
#if IGRAPH_INTEGER_SIZE == 64
204
static uint64_t igraph_i_rng_get_uint64(igraph_rng_t *rng);
205
static uint64_t igraph_i_rng_get_uint64_bounded(igraph_rng_t *rng, uint64_t range);
206
#endif
207
208
static double igraph_i_norm_rand(igraph_rng_t *rng);
209
static double igraph_i_exp_rand(igraph_rng_t *rng);
210
static double igraph_i_rbinom(igraph_rng_t *rng, igraph_integer_t n, double pp);
211
static double igraph_i_rexp(igraph_rng_t *rng, double rate);
212
static double igraph_i_rgamma(igraph_rng_t *rng, double shape, double scale);
213
static double igraph_i_rpois(igraph_rng_t *rng, double rate);
214
215
/**
216
 * \function igraph_rng_init
217
 * \brief Initializes a random number generator.
218
 *
219
 * This function allocates memory for a random number generator, with
220
 * the given type, and sets its seed to the default.
221
 *
222
 * \param rng Pointer to an uninitialized RNG.
223
 * \param type The type of the RNG, such as \ref igraph_rngtype_mt19937,
224
 * \ref igraph_rngtype_glibc2, \ref igraph_rngtype_pcg32 or
225
 * \ref igraph_rngtype_pcg64.
226
 * \return Error code.
227
 */
228
229
0
igraph_error_t igraph_rng_init(igraph_rng_t *rng, const igraph_rng_type_t *type) {
230
0
    rng->type = type;
231
0
    IGRAPH_CHECK(rng->type->init(&rng->state));
232
0
    return IGRAPH_SUCCESS;
233
0
}
234
235
/**
236
 * \function igraph_rng_destroy
237
 * \brief Deallocates memory associated with a random number generator.
238
 *
239
 * \param rng The RNG to destroy. Do not destroy an RNG that is used
240
 *    as the default igraph RNG.
241
 *
242
 * Time complexity: O(1).
243
 */
244
245
0
void igraph_rng_destroy(igraph_rng_t *rng) {
246
0
    rng->type->destroy(rng->state);
247
0
}
248
249
/**
250
 * \function igraph_rng_seed
251
 * \brief Seeds a random number generator.
252
 *
253
 * \param rng The RNG.
254
 * \param seed The new seed.
255
 * \return Error code.
256
 *
257
 * Time complexity: usually O(1), but may depend on the type of the
258
 * RNG.
259
 */
260
0
igraph_error_t igraph_rng_seed(igraph_rng_t *rng, igraph_uint_t seed) {
261
0
    const igraph_rng_type_t *type = rng->type;
262
0
    IGRAPH_CHECK(type->seed(rng->state, seed));
263
0
    rng->is_seeded = true;
264
0
    return IGRAPH_SUCCESS;
265
0
}
266
267
/**
268
 * \function igraph_rng_bits
269
 * \brief The number of random bits that a random number generator can produces in a single round.
270
 *
271
 * \param rng The RNG.
272
 * \return The number of random bits that can be generated in a single round
273
 *         with the RNG.
274
 *
275
 * Time complexity: O(1).
276
 */
277
0
igraph_integer_t igraph_rng_bits(const igraph_rng_t* rng) {
278
0
    return rng->type->bits;
279
0
}
280
281
/**
282
 * \function igraph_rng_max
283
 * \brief The maximum possible integer for a random number generator.
284
 *
285
 * Note that this number is only for informational purposes; it returns the
286
 * maximum possible integer that can be generated with the RNG with a single
287
 * call to its internals. It is derived directly from the number of random
288
 * \em bits that the RNG can generate in a single round. When this is smaller
289
 * than what would be needed by other RNG functions like \ref igraph_rng_get_integer(),
290
 * igraph will call the RNG multiple times to generate more random bits.
291
 *
292
 * \param rng The RNG.
293
 * \return The largest possible integer that can be generated in a single round
294
 *         with the RNG.
295
 *
296
 * Time complexity: O(1).
297
 */
298
299
0
igraph_uint_t igraph_rng_max(const igraph_rng_t *rng) {
300
0
    const igraph_rng_type_t *type = rng->type;
301
0
#if IGRAPH_INTEGER_SIZE == 64
302
0
    return (type->bits >= 64) ? 0xFFFFFFFFFFFFFFFFULL : ((1ULL << type->bits) - 1);
303
#else
304
    return (type->bits >= 32) ? 0xFFFFFFFFUL : ((1ULL << type->bits) - 1);
305
#endif
306
0
}
307
308
/**
309
 * \function igraph_rng_name
310
 * \brief The type of a random number generator.
311
 *
312
 * \param rng The RNG.
313
 * \return The name of the type of the generator. Do not deallocate or
314
 *         change the returned string.
315
 *
316
 * Time complexity: O(1).
317
 */
318
319
0
const char *igraph_rng_name(const igraph_rng_t *rng) {
320
0
    const igraph_rng_type_t *type = rng->type;
321
0
    return type->name;
322
0
}
323
324
/**
325
 * Generates a given number of random bits, possibly invoking the underlying
326
 * RNG multiple times if needed, and returns the result in an \c igraph_uint_t .
327
 *
328
 * \param rng The RNG.
329
 * \param bits The number of random bits needed. Must be smaller than or equal
330
 *        to the size of the \c igraph_uint_t data type. Passing a value larger
331
 *        than the size of \c igraph_uint_t will throw away random bits except
332
 *        the last few that are needed to fill an \c igraph_uint_t .
333
 * \return The random bits, packed into the low bits of an \c igraph_uint_t .
334
 *         The upper, unused bits of \c igraph_uint_t will be set to zero.
335
 */
336
0
static igraph_uint_t igraph_i_rng_get_random_bits(igraph_rng_t *rng, uint8_t bits) {
337
0
    const igraph_rng_type_t *type = rng->type;
338
0
    igraph_integer_t rng_bitwidth = igraph_rng_bits(rng);
339
0
    igraph_uint_t result;
340
341
0
    if (rng_bitwidth >= bits) {
342
        /* keep the high bits as RNGs sometimes tend to have lower entropy in
343
         * low bits than in high bits */
344
0
        result = type->get(rng->state) >> (rng_bitwidth - bits);
345
0
    } else {
346
0
        result = 0;
347
0
        do {
348
0
            result = (result << rng_bitwidth) + type->get(rng->state);
349
0
            bits -= rng_bitwidth;
350
0
        } while (bits > rng_bitwidth);
351
352
        /* and now the last piece */
353
0
        result = (result << bits) + (type->get(rng->state) >> (rng_bitwidth - bits));
354
0
    }
355
356
0
    return result;
357
0
}
358
359
/**
360
 * Generates a given number of random bits, possibly invoking the underlying
361
 * RNG multiple times if needed, and returns the result in an \c uint64_t .
362
 *
363
 * Prefer \c igraph_i_rng_get_random_bits() if you know that you need at most
364
 * 32 bits due to the type of the return value. This function might perform
365
 * worse on 32-bit platforms because the result is always 64 bits.
366
 *
367
 * \param rng The RNG.
368
 * \param bits The number of random bits needed. Must be smaller than or equal
369
 *        to the size of the \c uint64_t data type. Passing a value larger
370
 *        than the size of \c uint64_t will throw away random bits except
371
 *        the last few that are needed to fill an \c uint64_t .
372
 * \return The random bits, packed into the low bits of an \c uint64_t .
373
 *         The upper, unused bits of \c uint64_t will be set to zero.
374
 */
375
0
static uint64_t igraph_i_rng_get_random_bits_uint64(igraph_rng_t *rng, uint8_t bits) {
376
0
    const igraph_rng_type_t *type = rng->type;
377
0
    igraph_integer_t rng_bitwidth = igraph_rng_bits(rng);
378
0
    uint64_t result;
379
380
0
    if (rng_bitwidth >= bits) {
381
        /* keep the high bits as RNGs sometimes tend to have lower entropy in
382
         * low bits than in high bits */
383
0
        result = type->get(rng->state) >> (rng_bitwidth - bits);
384
0
    } else {
385
0
        result = 0;
386
0
        do {
387
0
            result = (result << rng_bitwidth) + type->get(rng->state);
388
0
            bits -= rng_bitwidth;
389
0
        } while (bits > rng_bitwidth);
390
391
        /* and now the last piece */
392
0
        result = (result << bits) + (type->get(rng->state) >> (rng_bitwidth - bits));
393
0
    }
394
395
0
    return result;
396
0
}
397
398
/**
399
 * Generates a random integer in the full range of the \c igraph_uint_t
400
 * data type.
401
 *
402
 * \param rng The RNG.
403
 * \return The random integer.
404
 */
405
0
static igraph_uint_t igraph_i_rng_get_uint(igraph_rng_t *rng) {
406
0
    return igraph_i_rng_get_random_bits(rng, sizeof(igraph_uint_t) * 8);
407
0
}
408
409
/**
410
 * Generates a random integer in the full range of the \c uint32_t
411
 * data type.
412
 *
413
 * \param rng The RNG.
414
 * \return The random integer.
415
 */
416
0
static uint32_t igraph_i_rng_get_uint32(igraph_rng_t *rng) {
417
0
    return igraph_i_rng_get_random_bits(rng, 32);
418
0
}
419
420
/**
421
 * Generates a random integer in the range [0; range) (upper bound exclusive),
422
 * restricted to at most 32 bits.
423
 *
424
 * \param rng The RNG.
425
 * \param range The upper bound (exclusive).
426
 * \return The random integer.
427
 */
428
0
static uint32_t igraph_i_rng_get_uint32_bounded(igraph_rng_t *rng, uint32_t range) {
429
    /* Debiased integer multiplication -- Lemire's method
430
     * from https://www.pcg-random.org/posts/bounded-rands.html */
431
0
    uint32_t x, l, t = (-range) % range;
432
0
    uint64_t m;
433
0
    do {
434
0
        x = igraph_i_rng_get_uint32(rng);
435
0
        m = (uint64_t)(x) * (uint64_t)(range);
436
0
        l = (uint32_t)m;
437
0
    } while (l < t);
438
0
    return m >> 32;
439
0
}
440
441
#if IGRAPH_INTEGER_SIZE == 64
442
/**
443
 * Generates a random integer in the full range of the \c uint64_t
444
 * data type.
445
 *
446
 * \param rng The RNG.
447
 * \param range The upper bound (inclusive).
448
 * \return The random integer.
449
 */
450
0
static uint64_t igraph_i_rng_get_uint64(igraph_rng_t *rng) {
451
0
    return igraph_i_rng_get_random_bits(rng, 64);
452
0
}
453
454
#if !defined(HAVE___UINT128_T)
455
static uint64_t igraph_i_umul128(uint64_t a, uint64_t b, uint64_t *hi) {
456
#if defined(HAVE__UMUL128)
457
    /* MSVC has _umul128() on x64 but not on arm64 */
458
    return _umul128(a, b, hi);
459
#elif defined(HAVE___UMULH)
460
    /* MSVC has __umulh() on arm64 */
461
    *hi = __umulh(a, b);
462
    return a*b;
463
#else
464
    /* Portable but slow fallback implementation of unsigned
465
     * 64-bit multiplication obtaining a 128-bit result.
466
     * Based on https://stackoverflow.com/a/28904636/695132
467
     */
468
469
    uint64_t a_lo = (uint32_t) a;
470
    uint64_t a_hi = a >> 32;
471
    uint64_t b_lo = (uint32_t) b;
472
    uint64_t b_hi = b >> 32;
473
474
    uint64_t a_x_b_hi  = a_hi * b_hi;
475
    uint64_t a_x_b_mid = a_hi * b_lo;
476
    uint64_t b_x_a_mid = b_hi * a_lo;
477
    uint64_t a_x_b_lo  = a_lo * b_lo;
478
479
    uint64_t carry_bit = ((uint64_t) (uint32_t) a_x_b_mid +
480
                          (uint64_t) (uint32_t) b_x_a_mid +
481
                          (a_x_b_lo >> 32) ) >> 32;
482
483
    *hi = a_x_b_hi +
484
          (a_x_b_mid >> 32) + (b_x_a_mid >> 32) +
485
          carry_bit;
486
487
    return a*b;
488
#endif
489
}
490
#endif /* !defined(HAVE___UINT128_T) */
491
492
/**
493
 * Generates a random integer in the range [0; range) (upper bound exclusive),
494
 * restricted to at most 64 bits.
495
 *
496
 * \param rng The RNG.
497
 * \param range The upper bound (exclusive).
498
 * \return The random integer.
499
 */
500
0
static uint64_t igraph_i_rng_get_uint64_bounded(igraph_rng_t *rng, uint64_t range) {
501
    /* Debiased integer multiplication -- Lemire's method
502
     * from https://www.pcg-random.org/posts/bounded-rands.html */
503
0
    uint64_t x, l, t = (-range) % range;
504
0
#if defined(HAVE___UINT128_T)
505
    /* gcc and clang have __uint128_t */
506
0
    __uint128_t m;
507
0
    do {
508
0
        x = igraph_i_rng_get_uint64(rng);
509
0
        m = (__uint128_t)(x) * (__uint128_t)(range);
510
0
        l = (uint64_t)m;
511
0
    } while (l < t);
512
0
    return m >> 64;
513
#else
514
    uint64_t hi;
515
    do {
516
        x = igraph_i_rng_get_uint64(rng);
517
        l = igraph_i_umul128(x, range, &hi);
518
    } while (l < t);
519
    return hi;
520
#endif
521
0
}
522
523
#endif /* IGRAPH_INTEGER_SIZE == 64 */
524
525
/**
526
 * Generates a random integer in the range [0; range) (upper bound exclusive).
527
 *
528
 * \param rng The RNG.
529
 * \param range The upper bound (exclusive).
530
 * \return The random integer.
531
 */
532
0
static igraph_uint_t igraph_i_rng_get_uint_bounded(igraph_rng_t *rng, igraph_uint_t range) {
533
    /* We must make this function behave the same way for range < 2^32 so igraph
534
     * behaves the same way on 32-bit and 64-bit platforms as long as we stick
535
     * to integers less than 2^32. This is to ensure that the unit tests are
536
     * consistent */
537
538
#if IGRAPH_INTEGER_SIZE == 32
539
    return igraph_i_rng_get_uint32_bounded(rng, range);
540
#else
541
0
    if (range <= UINT32_MAX) {
542
0
        return igraph_i_rng_get_uint32_bounded(rng, range);
543
0
    } else {
544
0
        return igraph_i_rng_get_uint64_bounded(rng, range);
545
0
    }
546
0
#endif
547
0
}
548
549
550
/**
551
 * \function igraph_rng_get_bool
552
 * \brief Generate a random boolean.
553
 *
554
 * Use this function only when a single random boolean, i.e. a single bit
555
 * is needed at a time. It is not efficient for generating multiple bits.
556
 *
557
 * \param rng Pointer to the RNG to use for the generation. Use \ref
558
 *        igraph_rng_default() here to use the default igraph RNG.
559
 * \return The generated bit, as a truth value.
560
 */
561
562
0
igraph_bool_t igraph_rng_get_bool(igraph_rng_t *rng) {
563
0
    const igraph_rng_type_t *type = rng->type;
564
0
    const igraph_integer_t rng_bitwidth = igraph_rng_bits(rng);
565
    /* Keep the highest bit as RNGs sometimes tend to have lower entropy in
566
     * low bits than in high bits.
567
     *
568
     * Ensure that the return value is stricly 0 or 1 even if igraph_bool_t is
569
     * defined to something else than a native bool.
570
     */
571
0
    return (type->get(rng->state) >> (rng_bitwidth - 1)) & (igraph_uint_t)1;
572
0
}
573
574
/**
575
 * \function igraph_rng_get_integer
576
 * \brief Generate an integer random number from an interval.
577
 *
578
 * Generate uniformly distributed integers from the interval <code>[l, h]</code>.
579
 *
580
 * \param rng Pointer to the RNG to use for the generation. Use \ref
581
 *        igraph_rng_default() here to use the default igraph RNG.
582
 * \param l Lower limit, inclusive, it can be negative as well.
583
 * \param h Upper limit, inclusive, it can be negative as well, but it
584
 *        must be at least <code>l</code>.
585
 * \return The generated random integer.
586
 *
587
 * Time complexity: O(log2(h-l+1) / bits) where bits is the value of
588
 * \ref igraph_rng_bits(rng).
589
 */
590
591
igraph_integer_t igraph_rng_get_integer(
592
    igraph_rng_t *rng, igraph_integer_t l, igraph_integer_t h
593
0
) {
594
0
    const igraph_rng_type_t *type = rng->type;
595
0
    igraph_uint_t range;
596
597
0
    assert(h >= l);
598
599
0
    if (h == l) {
600
0
        return l;
601
0
    }
602
603
0
    if (type->get_int) {
604
0
        return type->get_int(rng->state, l, h);
605
0
    }
606
607
0
    if (IGRAPH_UNLIKELY(l == IGRAPH_INTEGER_MIN && h == IGRAPH_INTEGER_MAX)) {
608
        /* Full uint range is needed, we can just grab a random number from
609
         * the uint range and cast it to a signed integer */
610
0
        return (igraph_integer_t) igraph_i_rng_get_uint(rng);
611
0
    } else if (l >= 0 || h < 0) {
612
        /* this is okay, (h - l) will not overflow an igraph_integer_t */
613
0
        range = (igraph_uint_t)(h - l) + 1;
614
0
    } else {
615
        /* (h - l) could potentially overflow so we need to play it safe. If we
616
         * are here, l < 0 and h >= 0 so we can cast -l into an igraph_uint_t
617
         * safely and do the subtraction that way */
618
0
        range = ((igraph_uint_t)(h)) + ((igraph_uint_t)(-l)) + 1;
619
0
    }
620
621
0
    return l + igraph_i_rng_get_uint_bounded(rng, range);
622
0
}
623
624
/**
625
 * \function igraph_rng_get_normal
626
 * \brief Samples from a normal distribution.
627
 *
628
 * Generates random variates from a normal distribution with probability
629
 * density
630
 *
631
 * </para><para>
632
 * <code>exp( -(x - m)^2 / (2 s^2) )</code>.
633
 *
634
 * \param rng Pointer to the RNG to use. Use \ref igraph_rng_default()
635
 *        here to use the default igraph RNG.
636
 * \param m The mean.
637
 * \param s The standard deviation.
638
 * \return The generated normally distributed random number.
639
 *
640
 * Time complexity: depends on the type of the RNG.
641
 */
642
643
igraph_real_t igraph_rng_get_normal(igraph_rng_t *rng,
644
0
                                    igraph_real_t m, igraph_real_t s) {
645
0
    const igraph_rng_type_t *type = rng->type;
646
0
    if (type->get_norm) {
647
0
        return type->get_norm(rng->state) * s + m;
648
0
    } else {
649
0
        return igraph_i_norm_rand(rng) * s + m;
650
0
    }
651
0
}
652
653
/**
654
 * \function igraph_rng_get_unif
655
 * \brief Samples real numbers from a given interval.
656
 *
657
 * Generates uniformly distributed real numbers from the <code>[l, h)</code>
658
 * half-open interval.
659
 *
660
 * \param rng Pointer to the RNG to use. Use \ref igraph_rng_default()
661
 *        here to use the default igraph RNG.
662
 * \param l The lower bound, it can be negative.
663
 * \param h The upper bound, it can be negative, but it has to be
664
 *        larger than the lower bound.
665
 * \return The generated uniformly distributed random number.
666
 *
667
 * Time complexity: depends on the type of the RNG.
668
 */
669
670
igraph_real_t igraph_rng_get_unif(igraph_rng_t *rng,
671
0
                                  igraph_real_t l, igraph_real_t h) {
672
0
    assert(h >= l);
673
674
0
    if (l == h) return h;
675
676
    /* Ensure that 'h' is never produced due to numerical roundoff errors, except when l == h. */
677
0
    igraph_real_t r;
678
0
    do {
679
0
        r = igraph_rng_get_unif01(rng) * (h - l) + l;
680
0
    } while (IGRAPH_UNLIKELY(r == h));
681
0
    return r;
682
0
}
683
684
/**
685
 * \function igraph_rng_get_unif01
686
 * \brief Samples uniformly from the unit interval.
687
 *
688
 * Generates uniformly distributed real numbers from the <code>[0, 1)</code>
689
 * half-open interval.
690
 *
691
 * \param rng Pointer to the RNG to use. Use \ref igraph_rng_default()
692
 *        here to use the default igraph RNG.
693
 * \return The generated uniformly distributed random number.
694
 *
695
 * Time complexity: depends on the type of the RNG.
696
 */
697
698
0
igraph_real_t igraph_rng_get_unif01(igraph_rng_t *rng) {
699
0
    const igraph_rng_type_t *type = rng->type;
700
0
    if (type->get_real) {
701
0
        return type->get_real(rng->state);
702
0
    } else {
703
        /* We extract 52 random bits from a 64-bit uint and fill that directly
704
         * into the mantissa of a double, bit-by-bit, clear the sign bit and
705
         * set the exponent to 2^0. This way we get a 52-bit random double
706
         * between 1 (inclusive) and 2 (exclusive), uniformly distributed.
707
         * Then we subtract 1 to arrive at the [0; 1) interval. This is fast
708
         * but we lose one bit of precision as there are 2^53 possible doubles
709
         * between 0 and 1. */
710
0
        union {
711
0
            uint64_t as_uint64_t;
712
0
            double as_double;
713
0
        } value;
714
0
        value.as_uint64_t =
715
0
            (igraph_i_rng_get_random_bits_uint64(rng, 52) & 0xFFFFFFFFFFFFFull) | 0x3FF0000000000000ull;
716
0
        return value.as_double - 1.0;
717
0
    }
718
0
}
719
720
/**
721
 * \function igraph_rng_get_geom
722
 * \brief Samples from a geometric distribution.
723
 *
724
 * Generates random variates from a geometric distribution. The number \c k is
725
 * generated with probability
726
 *
727
 * </para><para>
728
 * <code>(1 - p)^k p</code>, <code>k = 0, 1, 2, ...</code>.
729
 *
730
 * \param rng Pointer to the RNG to use. Use \ref igraph_rng_default()
731
 *        here to use the default igraph RNG.
732
 * \param p The probability of success in each trial. Must be larger
733
 *        than zero and smaller or equal to 1.
734
 * \return The generated geometrically distributed random number.
735
 *
736
 * Time complexity: depends on the RNG.
737
 */
738
739
0
igraph_real_t igraph_rng_get_geom(igraph_rng_t *rng, igraph_real_t p) {
740
0
    const igraph_rng_type_t *type = rng->type;
741
0
    if (!isfinite(p) || p <= 0 || p > 1) {
742
0
        return IGRAPH_NAN;
743
0
    }
744
0
    if (type->get_geom) {
745
0
        return type->get_geom(rng->state, p);
746
0
    } else {
747
0
        return igraph_rng_get_pois(rng, igraph_i_exp_rand(rng) * ((1 - p) / p));
748
0
    }
749
0
}
750
751
/**
752
 * \function igraph_rng_get_binom
753
 * \brief Samples from a binomial distribution.
754
 *
755
 * Generates random variates from a binomial distribution. The number \c k is generated
756
 * with probability
757
 *
758
 * </para><para>
759
 * <code>(n \choose k) p^k (1-p)^(n-k)</code>, <code>k = 0, 1, ..., n</code>.
760
 *
761
 * \param rng Pointer to the RNG to use. Use \ref igraph_rng_default()
762
 *        here to use the default igraph RNG.
763
 * \param n Number of observations.
764
 * \param p Probability of an event.
765
 * \return The generated binomially distributed random number.
766
 *
767
 * Time complexity: depends on the RNG.
768
 */
769
770
0
igraph_real_t igraph_rng_get_binom(igraph_rng_t *rng, igraph_integer_t n, igraph_real_t p) {
771
0
    const igraph_rng_type_t *type = rng->type;
772
0
    if (type->get_binom) {
773
0
        return type->get_binom(rng->state, n, p);
774
0
    } else {
775
0
        return igraph_i_rbinom(rng, n, p);
776
0
    }
777
0
}
778
779
/**
780
 * \function igraph_rng_get_gamma
781
 * \brief Samples from a gamma distribution.
782
 *
783
 * Generates random variates from a gamma distribution with probability
784
 * density proportional to
785
 *
786
 * </para><para>
787
 * <code>x^(shape-1) exp(-x / scale)</code>.
788
 *
789
 * \param rng Pointer to the RNG to use. Use \ref igraph_rng_default()
790
 *        here to use the default igraph RNG.
791
 * \param shape Shape parameter.
792
 * \param scale Scale parameter.
793
 * \return The generated sample.
794
 *
795
 * Time complexity: depends on the RNG.
796
 */
797
798
igraph_real_t igraph_rng_get_gamma(igraph_rng_t *rng, igraph_real_t shape,
799
0
                                   igraph_real_t scale) {
800
0
    const igraph_rng_type_t *type = rng->type;
801
0
    if (type->get_gamma) {
802
0
        return type->get_gamma(rng->state, shape, scale);
803
0
    } else {
804
0
        return igraph_i_rgamma(rng, shape, scale);
805
0
    }
806
0
}
807
808
/**
809
 * \function igraph_rng_get_exp
810
 * \brief Samples from an exponential distribution.
811
 *
812
 * Generates random variates from an exponential distribution with probability
813
 * density proportional to
814
 *
815
 * </para><para>
816
 * <code>exp(-rate x)</code>.
817
 *
818
 * \param rng Pointer to the RNG to use. Use \ref igraph_rng_default()
819
 *        here to use the default igraph RNG.
820
 * \param rate Rate parameter.
821
 * \return The generated sample.
822
 *
823
 * Time complexity: depends on the RNG.
824
 */
825
826
0
igraph_real_t igraph_rng_get_exp(igraph_rng_t *rng, igraph_real_t rate) {
827
0
    const igraph_rng_type_t *type = rng->type;
828
0
    if (type->get_exp) {
829
0
        return type->get_exp(rng->state, rate);
830
0
    } else {
831
0
        return igraph_i_rexp(rng, rate);
832
0
    }
833
0
}
834
835
/**
836
 * \function igraph_rng_get_pois
837
 * \brief Samples from a Poisson distribution.
838
 *
839
 * Generates random variates from a Poisson distribution. The number \c k is generated
840
 * with probability
841
 *
842
 * </para><para>
843
 * <code>rate^k * exp(-rate) / k!</code>, <code>k = 0, 1, 2, ...</code>.
844
 *
845
 * \param rng Pointer to the RNG to use. Use \ref igraph_rng_default()
846
 *        here to use the default igraph RNG.
847
 * \param rate The rate parameter of the Poisson distribution. Must not be negative.
848
 * \return The generated geometrically distributed random number.
849
 *
850
 * Time complexity: depends on the RNG.
851
 */
852
853
0
igraph_real_t igraph_rng_get_pois(igraph_rng_t *rng, igraph_real_t rate) {
854
0
    const igraph_rng_type_t *type = rng->type;
855
0
    if (isnan(rate) || rate < 0) {
856
0
        return IGRAPH_NAN;
857
0
    } else if (rate == 0) {
858
0
        return 0;
859
0
    } else if (type->get_pois) {
860
0
        return type->get_pois(rng->state, rate);
861
0
    } else {
862
0
        return igraph_i_rpois(rng, rate);
863
0
    }
864
0
}
865
866
867
/**
868
 * \ingroup internal
869
 *
870
 * This function appends the rest of the needed random numbers to the
871
 * result vector. It is Algoirthm A in Vitter's paper.
872
 */
873
874
static void igraph_i_random_sample_alga(igraph_vector_int_t *res,
875
                                        igraph_integer_t l, igraph_integer_t h,
876
0
                                        igraph_integer_t length) {
877
    /* Vitter: Variables V, quot, Nreal, and top are of type real */
878
879
0
    igraph_integer_t N = h - l + 1;
880
0
    igraph_integer_t n = length;
881
882
0
    igraph_real_t top = N - n;
883
0
    igraph_real_t Nreal = N;
884
0
    igraph_integer_t S = 0;
885
0
    igraph_real_t V, quot;
886
887
0
    l = l - 1;
888
889
0
    while (n >= 2) {
890
0
        V = RNG_UNIF01();
891
0
        S = 1;
892
0
        quot = top / Nreal;
893
0
        while (quot > V) {
894
0
            S += 1;
895
0
            top = -1.0 + top;
896
0
            Nreal = -1.0 + Nreal;
897
0
            quot = (quot * top) / Nreal;
898
0
        }
899
0
        l += S;
900
0
        igraph_vector_int_push_back(res, l); /* allocated */
901
0
        Nreal = -1.0 + Nreal; n = -1 + n;
902
0
    }
903
904
0
    S = trunc(round(Nreal) * RNG_UNIF01());
905
0
    l += S + 1;
906
0
    igraph_vector_int_push_back(res, l); /* allocated */
907
0
}
908
909
/**
910
 * \ingroup nongraph
911
 * \function igraph_random_sample
912
 * \brief Generates an increasing random sequence of integers.
913
 *
914
 * This function generates an increasing sequence of random integer
915
 * numbers from a given interval. The algorithm is taken literally
916
 * from (Vitter 1987). This method can be used for generating numbers from a
917
 * \em very large interval. It is primarily created for randomly
918
 * selecting some edges from the sometimes huge set of possible edges
919
 * in a large graph.
920
 *
921
 * </para><para>
922
 * Reference:
923
 *
924
 * </para><para>
925
 * J. S. Vitter. An efficient algorithm for sequential random sampling.
926
 * ACM Transactions on Mathematical Software, 13(1):58--67, 1987.
927
 * https://doi.org/10.1145/23002.23003
928
 *
929
 * \param res Pointer to an initialized vector. This will hold the
930
 *        result. It will be resized to the proper size.
931
 * \param l The lower limit of the generation interval (inclusive). This must
932
 *        be less than or equal to the upper limit, and it must be integral.
933
 * \param h The upper limit of the generation interval (inclusive). This must
934
 *        be greater than or equal to the lower limit, and it must be integral.
935
 * \param length The number of random integers to generate.
936
 * \return The error code \c IGRAPH_EINVAL is returned in each of the
937
 *         following cases: (1) The given lower limit is greater than the
938
 *         given upper limit, i.e. \c l &gt; \c h. (2) Assuming that
939
 *         \c l &lt; \c h and N is the sample size, the above error code is
940
 *         returned if N &gt; |\c h - \c l|, i.e. the sample size exceeds the
941
 *         size of the candidate pool.
942
 *
943
 * Time complexity: according to (Vitter 1987), the expected
944
 * running time is O(length).
945
 *
946
 * \example examples/simple/igraph_random_sample.c
947
 */
948
949
igraph_error_t igraph_random_sample(igraph_vector_int_t *res, igraph_integer_t l, igraph_integer_t h,
950
0
                         igraph_integer_t length) {
951
0
    igraph_integer_t N; /* := h - l + 1 */
952
0
    IGRAPH_SAFE_ADD(h, -l, &N);
953
0
    IGRAPH_SAFE_ADD(N, 1, &N);
954
955
0
    igraph_integer_t n = length;
956
957
0
    igraph_real_t nreal = length;
958
0
    igraph_real_t ninv = (nreal != 0) ? 1.0 / nreal : 0.0;
959
0
    igraph_real_t Nreal = N;
960
0
    igraph_real_t Vprime;
961
0
    igraph_integer_t qu1 = -n + 1 + N;
962
0
    igraph_real_t qu1real = -nreal + 1.0 + Nreal;
963
0
    igraph_real_t negalphainv = -13;
964
0
    igraph_real_t threshold = -negalphainv * n;
965
0
    igraph_integer_t S;
966
967
    /* getting back some sense of sanity */
968
0
    if (l > h) {
969
0
        IGRAPH_ERROR("Lower limit is greater than upper limit.", IGRAPH_EINVAL);
970
0
    }
971
    /* now we know that l <= h */
972
0
    if (length > N) {
973
0
        IGRAPH_ERROR("Sample size exceeds size of candidate pool.", IGRAPH_EINVAL);
974
0
    }
975
976
    /* treat rare cases quickly */
977
0
    if (l == h) {
978
0
        IGRAPH_CHECK(igraph_vector_int_resize(res, 1));
979
0
        VECTOR(*res)[0] = l;
980
0
        return IGRAPH_SUCCESS;
981
0
    }
982
0
    if (length == 0) {
983
0
        igraph_vector_int_clear(res);
984
0
        return IGRAPH_SUCCESS;
985
0
    }
986
0
    if (length == N) {
987
0
        IGRAPH_CHECK(igraph_vector_int_resize(res, length));
988
0
        for (igraph_integer_t i = 0; i < length; i++) {
989
0
            VECTOR(*res)[i] = l++;
990
0
        }
991
0
        return IGRAPH_SUCCESS;
992
0
    }
993
994
0
    igraph_vector_int_clear(res);
995
0
    IGRAPH_CHECK(igraph_vector_int_reserve(res, length));
996
997
0
    RNG_BEGIN();
998
999
0
    Vprime = exp(log(RNG_UNIF01()) * ninv);
1000
0
    l = l - 1;
1001
1002
0
    while (n > 1 && threshold < N) {
1003
0
        igraph_real_t X, U;
1004
0
        igraph_real_t limit, t;
1005
0
        igraph_real_t negSreal, y1, y2, top, bottom;
1006
0
        igraph_real_t nmin1inv = 1.0 / (-1.0 + nreal);
1007
0
        while (1) {
1008
0
            while (1) {
1009
0
                X = Nreal * (-Vprime + 1.0);
1010
0
                S = floor(X);
1011
                /* if (S==0) { S=1; } */
1012
0
                if (S < qu1) {
1013
0
                    break;
1014
0
                }
1015
0
                Vprime = exp(log(RNG_UNIF01()) * ninv);
1016
0
            }
1017
0
            U = RNG_UNIF01();
1018
0
            negSreal = -S;
1019
1020
0
            y1 = exp(log(U * Nreal / qu1real) * nmin1inv);
1021
0
            Vprime = y1 * (-X / Nreal + 1.0) * (qu1real / (negSreal + qu1real));
1022
0
            if (Vprime <= 1.0) {
1023
0
                break;
1024
0
            }
1025
1026
0
            y2 = 1.0;
1027
0
            top = -1.0 + Nreal;
1028
0
            if (-1 + n > S) {
1029
0
                bottom = -nreal + Nreal;
1030
0
                limit = -S + N;
1031
0
            } else {
1032
0
                bottom = -1.0 + negSreal + Nreal;
1033
0
                limit = qu1;
1034
0
            }
1035
0
            for (t = -1 + N; t >= limit; t--) {
1036
0
                y2 = (y2 * top) / bottom;
1037
0
                top = -1.0 + top;
1038
0
                bottom = -1.0 + bottom;
1039
0
            }
1040
0
            if (Nreal / (-X + Nreal) >= y1 * exp(log(y2)*nmin1inv)) {
1041
0
                Vprime = exp(log(RNG_UNIF01()) * nmin1inv);
1042
0
                break;
1043
0
            }
1044
0
            Vprime = exp(log(RNG_UNIF01()) * ninv);
1045
0
        }
1046
1047
0
        l += S + 1;
1048
0
        igraph_vector_int_push_back(res, l);    /* allocated */
1049
0
        N = -S + (-1 + N);   Nreal = negSreal + (-1.0 + Nreal);
1050
0
        n = -1 + n;   nreal = -1.0 + nreal; ninv = nmin1inv;
1051
0
        qu1 = -S + qu1; qu1real = negSreal + qu1real;
1052
0
        threshold = threshold + negalphainv;
1053
0
    }
1054
1055
0
    if (n > 1) {
1056
0
        igraph_i_random_sample_alga(res, l + 1, h, n);
1057
0
    } else {
1058
0
        S = floor(N * Vprime);
1059
0
        l += S + 1;
1060
0
        igraph_vector_int_push_back(res, l);    /* allocated */
1061
0
    }
1062
1063
0
    RNG_END();
1064
1065
0
    return IGRAPH_SUCCESS;
1066
0
}
1067
1068
static void igraph_i_random_sample_alga_real(igraph_vector_t *res,
1069
                                       igraph_real_t l, igraph_real_t h,
1070
0
                                       igraph_real_t length) {
1071
0
    igraph_real_t N = h - l + 1;
1072
0
    igraph_real_t n = length;
1073
1074
0
    igraph_real_t top = N - n;
1075
0
    igraph_real_t Nreal = N;
1076
0
    igraph_real_t S = 0;
1077
0
    igraph_real_t V, quot;
1078
1079
0
    l = l - 1;
1080
1081
0
    while (n >= 2) {
1082
0
        V = RNG_UNIF01();
1083
0
        S = 1;
1084
0
        quot = top / Nreal;
1085
0
        while (quot > V) {
1086
0
            S += 1;
1087
0
            top = -1.0 + top;
1088
0
            Nreal = -1.0 + Nreal;
1089
0
            quot = (quot * top) / Nreal;
1090
0
        }
1091
0
        l += S;
1092
0
        igraph_vector_push_back(res, l); /* allocated */
1093
0
        Nreal = -1.0 + Nreal; n = -1 + n;
1094
0
    }
1095
1096
0
    S = trunc(round(Nreal) * RNG_UNIF01());
1097
0
    l += S + 1;
1098
0
    igraph_vector_push_back(res, l); /* allocated */
1099
0
}
1100
1101
/**
1102
 * \ingroup nongraph
1103
 * \function igraph_i_random_sample_real
1104
 * \brief Generates an increasing random sequence of integers (igraph_real_t version).
1105
 *
1106
 * This function is the 'real' version of \ref igraph_random_sample(), and was added
1107
 * so \ref igraph_erdos_renyi_game_gnm() and related functions can use a random sample
1108
 * of doubles instead of integers to prevent overflows on systems with 32-bit
1109
 * \type igraph_integer_t.
1110
 *
1111
 * \param res Pointer to an initialized vector. This will hold the
1112
 *        result. It will be resized to the proper size.
1113
 * \param l The lower limit of the generation interval (inclusive). This must
1114
 *        be less than or equal to the upper limit, and it must be integral.
1115
 *        Passing a fractional number here results in undefined behaviour.
1116
 * \param h The upper limit of the generation interval (inclusive). This must
1117
 *        be greater than or equal to the lower limit, and it must be integral.
1118
 *        Passing a fractional number here results in undefined behaviour.
1119
 * \param length The number of random integers to generate.
1120
 * \return The error code \c IGRAPH_EINVAL is returned in each of the
1121
 *         following cases: (1) The given lower limit is greater than the
1122
 *         given upper limit, i.e. \c l &gt; \c h. (2) Assuming that
1123
 *         \c l &lt; \c h and N is the sample size, the above error code is
1124
 *         returned if N &gt; |\c h - \c l|, i.e. the sample size exceeds the
1125
 *         size of the candidate pool.
1126
 */
1127
1128
igraph_error_t igraph_i_random_sample_real(igraph_vector_t *res, igraph_real_t l,
1129
0
                    igraph_real_t h, igraph_integer_t length) {
1130
    /* This function is the 'real' version of igraph_random_sample, and was added
1131
     * so erdos_renyi_game_gnm can use a random sample of doubles instead of integers
1132
     * to prevent overflows on systems with 32-bits igraph_integer_t.
1133
     */
1134
0
    igraph_real_t N = h - l + 1;
1135
0
    igraph_real_t n = length;
1136
1137
0
    igraph_real_t nreal = length;
1138
0
    igraph_real_t ninv = (nreal != 0) ? 1.0 / nreal : 0.0;
1139
0
    igraph_real_t Nreal = N;
1140
0
    igraph_real_t Vprime;
1141
0
    igraph_real_t qu1 = -n + 1 + N;
1142
0
    igraph_real_t qu1real = -nreal + 1.0 + Nreal;
1143
0
    igraph_real_t negalphainv = -13;
1144
0
    igraph_real_t threshold = -negalphainv * n;
1145
0
    igraph_real_t S;
1146
0
    int iter = 0;
1147
1148
    /* getting back some sense of sanity */
1149
0
    if (l > h) {
1150
0
        IGRAPH_ERROR("Lower limit is greater than upper limit.", IGRAPH_EINVAL);
1151
0
    }
1152
    /* now we know that l <= h */
1153
0
    if (length > N) {
1154
0
        IGRAPH_ERROR("Sample size exceeds size of candidate pool.", IGRAPH_EINVAL);
1155
0
    }
1156
1157
    /* ensure that we work in the range where igraph_real_t can represent integers exactly */
1158
0
    if (h > IGRAPH_MAX_EXACT_REAL || l < -IGRAPH_MAX_EXACT_REAL || N > IGRAPH_MAX_EXACT_REAL) {
1159
0
        IGRAPH_ERROR("Sampling interval too large.", IGRAPH_EOVERFLOW);
1160
0
    }
1161
1162
    /* treat rare cases quickly */
1163
0
    if (l == h) {
1164
0
        IGRAPH_CHECK(igraph_vector_resize(res, 1));
1165
0
        VECTOR(*res)[0] = l;
1166
0
        return IGRAPH_SUCCESS;
1167
0
    }
1168
0
    if (length == 0) {
1169
0
        igraph_vector_clear(res);
1170
0
        return IGRAPH_SUCCESS;
1171
0
    }
1172
0
    if (length == N) {
1173
0
        IGRAPH_CHECK(igraph_vector_resize(res, length));
1174
0
        for (igraph_integer_t i = 0; i < length; i++) {
1175
0
            VECTOR(*res)[i] = l++;
1176
0
        }
1177
0
        return IGRAPH_SUCCESS;
1178
0
    }
1179
1180
0
    igraph_vector_clear(res);
1181
0
    IGRAPH_CHECK(igraph_vector_reserve(res, length));
1182
1183
0
    RNG_BEGIN();
1184
1185
0
    Vprime = exp(log(RNG_UNIF01()) * ninv);
1186
0
    l = l - 1;
1187
1188
0
    while (n > 1 && threshold < N) {
1189
0
        igraph_real_t X, U;
1190
0
        igraph_real_t limit, t;
1191
0
        igraph_real_t negSreal, y1, y2, top, bottom;
1192
0
        igraph_real_t nmin1inv = 1.0 / (-1.0 + nreal);
1193
0
        while (1) {
1194
0
            while (1) {
1195
0
                X = Nreal * (-Vprime + 1.0);
1196
0
                S = floor(X);
1197
                /* if (S==0) { S=1; } */
1198
0
                if (S < qu1) {
1199
0
                    break;
1200
0
                }
1201
0
                Vprime = exp(log(RNG_UNIF01()) * ninv);
1202
0
            }
1203
0
            U = RNG_UNIF01();
1204
0
            negSreal = -S;
1205
1206
0
            y1 = exp(log(U * Nreal / qu1real) * nmin1inv);
1207
0
            Vprime = y1 * (-X / Nreal + 1.0) * (qu1real / (negSreal + qu1real));
1208
0
            if (Vprime <= 1.0) {
1209
0
                break;
1210
0
            }
1211
1212
0
            y2 = 1.0;
1213
0
            top = -1.0 + Nreal;
1214
0
            if (-1 + n > S) {
1215
0
                bottom = -nreal + Nreal;
1216
0
                limit = -S + N;
1217
0
            } else {
1218
0
                bottom = -1.0 + negSreal + Nreal;
1219
0
                limit = qu1;
1220
0
            }
1221
0
            for (t = -1 + N; t >= limit; t--) {
1222
0
                y2 = (y2 * top) / bottom;
1223
0
                top = -1.0 + top;
1224
0
                bottom = -1.0 + bottom;
1225
0
            }
1226
0
            if (Nreal / (-X + Nreal) >= y1 * exp(log(y2)*nmin1inv)) {
1227
0
                Vprime = exp(log(RNG_UNIF01()) * nmin1inv);
1228
0
                break;
1229
0
            }
1230
0
            Vprime = exp(log(RNG_UNIF01()) * ninv);
1231
0
        }
1232
1233
0
        l += S + 1;
1234
0
        igraph_vector_push_back(res, l);    /* allocated */
1235
0
        N = -S + (-1 + N);   Nreal = negSreal + (-1.0 + Nreal);
1236
0
        n = -1 + n;   nreal = -1.0 + nreal; ninv = nmin1inv;
1237
0
        qu1 = -S + qu1; qu1real = negSreal + qu1real;
1238
0
        threshold = threshold + negalphainv;
1239
1240
0
        if (++iter >= (1 << 14)) {
1241
0
            iter = 0;
1242
0
            IGRAPH_ALLOW_INTERRUPTION();
1243
0
        }
1244
0
    }
1245
1246
0
    if (n > 1) {
1247
0
        igraph_i_random_sample_alga_real(res, l + 1, h, n);
1248
0
    } else {
1249
0
        S = floor(N * Vprime);
1250
0
        l += S + 1;
1251
0
        igraph_vector_push_back(res, l);    /* allocated */
1252
0
    }
1253
1254
0
    RNG_END();
1255
1256
0
    return IGRAPH_SUCCESS;
1257
0
}
1258
1259
/*
1260
 *  Mathlib : A C Library of Special Functions
1261
 *  Copyright (C) 1998 Ross Ihaka
1262
 *  Copyright (C) 2000 The R Development Core Team
1263
 *  based on AS 111 (C) 1977 Royal Statistical Society
1264
 *  and   on AS 241 (C) 1988 Royal Statistical Society
1265
 *
1266
 *  This program is free software; you can redistribute it and/or modify
1267
 *  it under the terms of the GNU General Public License as published by
1268
 *  the Free Software Foundation; either version 2 of the License, or
1269
 *  (at your option) any later version.
1270
 *
1271
 *  This program is distributed in the hope that it will be useful,
1272
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
1273
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1274
 *  GNU General Public License for more details.
1275
 *
1276
 *  You should have received a copy of the GNU General Public License
1277
 *  along with this program; if not, write to the Free Software
1278
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA.
1279
 *
1280
 *  SYNOPSIS
1281
 *
1282
 *  double qnorm5(double p, double mu, double sigma,
1283
 *            int lower_tail, int log_p)
1284
 *            {qnorm (..) is synonymous and preferred inside R}
1285
 *
1286
 *  DESCRIPTION
1287
 *
1288
 *  Compute the quantile function for the normal distribution.
1289
 *
1290
 *  For small to moderate probabilities, algorithm referenced
1291
 *  below is used to obtain an initial approximation which is
1292
 *  polished with a final Newton step.
1293
 *
1294
 *  For very large arguments, an algorithm of Wichura is used.
1295
 *
1296
 *  REFERENCE
1297
 *
1298
 *  Beasley, J. D. and S. G. Springer (1977).
1299
 *  Algorithm AS 111: The percentage points of the normal distribution,
1300
 *  Applied Statistics, 26, 118-121.
1301
 *
1302
 *      Wichura, M.J. (1988).
1303
 *      Algorithm AS 241: The Percentage Points of the Normal Distribution.
1304
 *      Applied Statistics, 37, 477-484.
1305
 */
1306
1307
/*
1308
 *  Mathlib : A C Library of Special Functions
1309
 *  Copyright (C) 1998-2004  The R Development Core Team
1310
 *
1311
 *  This program is free software; you can redistribute it and/or modify
1312
 *  it under the terms of the GNU General Public License as published by
1313
 *  the Free Software Foundation; either version 2 of the License, or
1314
 *  (at your option) any later version.
1315
 *
1316
 *  This program is distributed in the hope that it will be useful,
1317
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
1318
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1319
 *  GNU General Public License for more details.
1320
 *
1321
 *  You should have received a copy of the GNU General Public License
1322
 *  along with this program; if not, write to the Free Software
1323
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1324
 *
1325
 */
1326
1327
/* The ISNAN macro is used in some of the code borrowed from R below. */
1328
0
#define ISNAN isnan
1329
1330
/* Indicates that we use systems which support NaN values. */
1331
#define IEEE_754 1
1332
1333
/* Private header file for use during compilation of Mathlib */
1334
#ifndef MATHLIB_PRIVATE_H
1335
#define MATHLIB_PRIVATE_H
1336
1337
0
#define ML_POSINF IGRAPH_INFINITY
1338
0
#define ML_NEGINF -IGRAPH_INFINITY
1339
0
#define ML_NAN    IGRAPH_NAN
1340
1341
#define ML_ERROR(x) /* nothing */
1342
#define ML_UNDERFLOW    (DBL_MIN * DBL_MIN)
1343
#define ML_VALID(x) (!ISNAN(x))
1344
1345
#define ME_NONE     0
1346
/*  no error */
1347
#define ME_DOMAIN   1
1348
/*  argument out of domain */
1349
#define ME_RANGE    2
1350
/*  value out of range */
1351
#define ME_NOCONV   4
1352
/*  process did not converge */
1353
#define ME_PRECISION    8
1354
/*  does not have "full" precision */
1355
#define ME_UNDERFLOW    16
1356
/*  and underflow occurred (important for IEEE)*/
1357
1358
0
#define ML_ERR_return_NAN { ML_ERROR(ME_DOMAIN); return ML_NAN; }
1359
1360
#endif /* MATHLIB_PRIVATE_H */
1361
1362
1363
/* Utilities for `dpq' handling (density/probability/quantile) */
1364
1365
/* give_log in "d";  log_p in "p" & "q" : */
1366
#define give_log log_p
1367
/* "DEFAULT" */
1368
/* --------- */
1369
0
#define R_D__0  (log_p ? ML_NEGINF : 0.)        /* 0 */
1370
0
#define R_D__1  (log_p ? 0. : 1.)           /* 1 */
1371
0
#define R_DT_0  (lower_tail ? R_D__0 : R_D__1)      /* 0 */
1372
0
#define R_DT_1  (lower_tail ? R_D__1 : R_D__0)      /* 1 */
1373
1374
0
#define R_D_Lval(p) (lower_tail ? (p) : (1 - (p)))  /*  p  */
1375
0
#define R_D_Cval(p) (lower_tail ? (1 - (p)) : (p))  /*  1 - p */
1376
1377
#define R_D_val(x)  (log_p  ? log(x) : (x))     /*  x  in pF(x,..) */
1378
#define R_D_qIv(p)  (log_p  ? exp(p) : (p))     /*  p  in qF(p,..) */
1379
#define R_D_exp(x)  (log_p  ?  (x)   : exp(x))  /* exp(x) */
1380
#define R_D_log(p)  (log_p  ?  (p)   : log(p))  /* log(p) */
1381
#define R_D_Clog(p) (log_p  ? log1p(-(p)) : (1 - (p)))/* [log](1-p) */
1382
1383
/* log(1-exp(x)):  R_D_LExp(x) == (log1p(- R_D_qIv(x))) but even more stable:*/
1384
#define R_D_LExp(x)     (log_p ? R_Log1_Exp(x) : log1p(-x))
1385
1386
/*till 1.8.x:
1387
 * #define R_DT_val(x)  R_D_val(R_D_Lval(x))
1388
 * #define R_DT_Cval(x) R_D_val(R_D_Cval(x)) */
1389
#define R_DT_val(x) (lower_tail ? R_D_val(x)  : R_D_Clog(x))
1390
#define R_DT_Cval(x)    (lower_tail ? R_D_Clog(x) : R_D_val(x))
1391
1392
/*#define R_DT_qIv(p)   R_D_Lval(R_D_qIv(p))         *  p  in qF ! */
1393
0
#define R_DT_qIv(p) (log_p ? (lower_tail ? exp(p) : - expm1(p)) \
1394
0
                     : R_D_Lval(p))
1395
1396
/*#define R_DT_CIv(p)   R_D_Cval(R_D_qIv(p))         *  1 - p in qF */
1397
0
#define R_DT_CIv(p) (log_p ? (lower_tail ? -expm1(p) : exp(p)) \
1398
0
                     : R_D_Cval(p))
1399
1400
#define R_DT_exp(x) R_D_exp(R_D_Lval(x))        /* exp(x) */
1401
#define R_DT_Cexp(x)    R_D_exp(R_D_Cval(x))        /* exp(1 - x) */
1402
1403
#define R_DT_log(p) (lower_tail? R_D_log(p) : R_D_LExp(p))/* log(p) in qF */
1404
#define R_DT_Clog(p)    (lower_tail? R_D_LExp(p): R_D_log(p))/* log(1-p) in qF*/
1405
#define R_DT_Log(p) (lower_tail? (p) : R_Log1_Exp(p))
1406
/* ==   R_DT_log when we already "know" log_p == true :*/
1407
1408
#define R_Q_P01_check(p)            \
1409
0
    if ((log_p  && p > 0) ||            \
1410
0
        (!log_p && (p < 0 || p > 1)) )      \
1411
0
        ML_ERR_return_NAN
1412
1413
/* additions for density functions (C.Loader) */
1414
#define R_D_fexp(f,x)     (give_log ? -0.5*log(f)+(x) : exp(x)/sqrt(f))
1415
#define R_D_forceint(x)   floor((x) + 0.5)
1416
#define R_D_nonint(x)     (fabs((x) - floor((x)+0.5)) > 1e-7)
1417
/* [neg]ative or [non int]eger : */
1418
#define R_D_negInonint(x) (x < 0. || R_D_nonint(x))
1419
1420
#define R_D_nonint_check(x)                 \
1421
    if (R_D_nonint(x)) {                  \
1422
        MATHLIB_WARNING("non-integer x = %f", x);   \
1423
        return R_D__0;                  \
1424
    }
1425
1426
0
static double igraph_i_qnorm5(double p, double mu, double sigma, igraph_bool_t lower_tail, igraph_bool_t log_p) {
1427
0
    double p_, q, r, val;
1428
1429
0
#ifdef IEEE_754
1430
0
    if (ISNAN(p) || ISNAN(mu) || ISNAN(sigma)) {
1431
0
        return p + mu + sigma;
1432
0
    }
1433
0
#endif
1434
0
    if (p == R_DT_0) {
1435
0
        return ML_NEGINF;
1436
0
    }
1437
0
    if (p == R_DT_1) {
1438
0
        return ML_POSINF;
1439
0
    }
1440
0
    R_Q_P01_check(p);
1441
1442
0
    if (sigma  < 0) {
1443
0
        ML_ERR_return_NAN;
1444
0
    }
1445
0
    if (sigma == 0) {
1446
0
        return mu;
1447
0
    }
1448
1449
0
    p_ = R_DT_qIv(p);/* real lower_tail prob. p */
1450
0
    q = p_ - 0.5;
1451
1452
    /*-- use AS 241 --- */
1453
    /* double ppnd16_(double *p, long *ifault)*/
1454
    /*      ALGORITHM AS241  APPL. STATIST. (1988) VOL. 37, NO. 3
1455
1456
            Produces the normal deviate Z corresponding to a given lower
1457
            tail area of P; Z is accurate to about 1 part in 10**16.
1458
1459
            (original fortran code used PARAMETER(..) for the coefficients
1460
             and provided hash codes for checking them...)
1461
    */
1462
0
    if (fabs(q) <= .425) {/* 0.075 <= p <= 0.925 */
1463
0
        r = .180625 - q * q;
1464
0
        val =
1465
0
            q * (((((((r * 2509.0809287301226727 +
1466
0
                       33430.575583588128105) * r + 67265.770927008700853) * r +
1467
0
                     45921.953931549871457) * r + 13731.693765509461125) * r +
1468
0
                   1971.5909503065514427) * r + 133.14166789178437745) * r +
1469
0
                 3.387132872796366608)
1470
0
            / (((((((r * 5226.495278852854561 +
1471
0
                     28729.085735721942674) * r + 39307.89580009271061) * r +
1472
0
                   21213.794301586595867) * r + 5394.1960214247511077) * r +
1473
0
                 687.1870074920579083) * r + 42.313330701600911252) * r + 1.);
1474
0
    } else { /* closer than 0.075 from {0,1} boundary */
1475
1476
        /* r = min(p, 1-p) < 0.075 */
1477
0
        if (q > 0) {
1478
0
            r = R_DT_CIv(p);    /* 1-p */
1479
0
        } else {
1480
0
            r = p_;    /* = R_DT_Iv(p) ^=  p */
1481
0
        }
1482
1483
0
        r = sqrt(- ((log_p &&
1484
0
                     ((lower_tail && q <= 0) || (!lower_tail && q > 0))) ?
1485
0
                    p : /* else */ log(r)));
1486
        /* r = sqrt(-log(r))  <==>  min(p, 1-p) = exp( - r^2 ) */
1487
1488
0
        if (r <= 5.) { /* <==> min(p,1-p) >= exp(-25) ~= 1.3888e-11 */
1489
0
            r += -1.6;
1490
0
            val = (((((((r * 7.7454501427834140764e-4 +
1491
0
                         .0227238449892691845833) * r + .24178072517745061177) *
1492
0
                       r + 1.27045825245236838258) * r +
1493
0
                      3.64784832476320460504) * r + 5.7694972214606914055) *
1494
0
                    r + 4.6303378461565452959) * r +
1495
0
                   1.42343711074968357734)
1496
0
                  / (((((((r *
1497
0
                           1.05075007164441684324e-9 + 5.475938084995344946e-4) *
1498
0
                          r + .0151986665636164571966) * r +
1499
0
                         .14810397642748007459) * r + .68976733498510000455) *
1500
0
                       r + 1.6763848301838038494) * r +
1501
0
                      2.05319162663775882187) * r + 1.);
1502
0
        } else { /* very close to  0 or 1 */
1503
0
            r += -5.;
1504
0
            val = (((((((r * 2.01033439929228813265e-7 +
1505
0
                         2.71155556874348757815e-5) * r +
1506
0
                        .0012426609473880784386) * r + .026532189526576123093) *
1507
0
                      r + .29656057182850489123) * r +
1508
0
                     1.7848265399172913358) * r + 5.4637849111641143699) *
1509
0
                   r + 6.6579046435011037772)
1510
0
                  / (((((((r *
1511
0
                           2.04426310338993978564e-15 + 1.4215117583164458887e-7) *
1512
0
                          r + 1.8463183175100546818e-5) * r +
1513
0
                         7.868691311456132591e-4) * r + .0148753612908506148525)
1514
0
                       * r + .13692988092273580531) * r +
1515
0
                      .59983220655588793769) * r + 1.);
1516
0
        }
1517
1518
0
        if (q < 0.0) {
1519
0
            val = -val;
1520
0
        }
1521
        /* return (q >= 0.)? r : -r ;*/
1522
0
    }
1523
0
    return mu + sigma * val;
1524
0
}
1525
1526
0
static igraph_integer_t imax2(igraph_integer_t x, igraph_integer_t y) {
1527
0
    return (x < y) ? y : x;
1528
0
}
1529
1530
0
static igraph_integer_t imin2(igraph_integer_t x, igraph_integer_t y) {
1531
0
    return (x < y) ? x : y;
1532
0
}
1533
1534
0
static double igraph_i_norm_rand(igraph_rng_t *rng) {
1535
0
    double r;
1536
1537
    /* Use the inversion method based on uniform variates from (0, 1).
1538
     * We exclude 0.0 as it would lead to generating -infinity.
1539
     * It is assumed that unif01() provides sufficient accuracy.
1540
     * A resolution of 2^-32 may not be sufficient. igraph's default
1541
     * implementaton provides an accuracy of 2^-52.
1542
     */
1543
0
    do {
1544
0
        r = igraph_rng_get_unif01(rng);
1545
0
    } while (r == 0.0);
1546
1547
0
    return igraph_i_qnorm5(r, 0.0, 1.0, true, false);
1548
0
}
1549
1550
/*
1551
 * The following function is igraph code (not R / Mathlib).
1552
 *
1553
 * We use simple inverse transform sampling, with the assumption that the
1554
 * quality/resolution of uniform variates is high (52 bits in the default
1555
 * implementation). The quantile function is -log(1 - r) but given that
1556
 * r is sampled uniformly form the unit interval, -log(r) is equivalent.
1557
 * r = 0 is disallowed as it would yield infinity.
1558
 */
1559
1560
0
static double igraph_i_exp_rand(igraph_rng_t *rng) {
1561
0
    igraph_real_t r = igraph_rng_get_unif01(rng);
1562
0
    if (r == 0.0) r = 1.0; /* sample from (0, 1] instead of [0, 1) */
1563
0
    return -log(r);
1564
0
}
1565
1566
/*
1567
 *  Mathlib : A C Library of Special Functions
1568
 *  Copyright (C) 1998 Ross Ihaka
1569
 *  Copyright (C) 2000-2001 The R Development Core Team
1570
 *
1571
 *  This program is free software; you can redistribute it and/or modify
1572
 *  it under the terms of the GNU General Public License as published by
1573
 *  the Free Software Foundation; either version 2 of the License, or
1574
 *  (at your option) any later version.
1575
 *
1576
 *  This program is distributed in the hope that it will be useful,
1577
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
1578
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1579
 *  GNU General Public License for more details.
1580
 *
1581
 *  You should have received a copy of the GNU General Public License
1582
 *  along with this program; if not, write to the Free Software
1583
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA.
1584
 *
1585
 *  SYNOPSIS
1586
 *
1587
 *    #include <Rmath.h>
1588
 *    double rpois(double lambda)
1589
 *
1590
 *  DESCRIPTION
1591
 *
1592
 *    Random variates from the Poisson distribution.
1593
 *
1594
 *  REFERENCE
1595
 *
1596
 *    Ahrens, J.H. and Dieter, U. (1982).
1597
 *    Computer generation of Poisson deviates
1598
 *    from modified normal distributions.
1599
 *    ACM Trans. Math. Software 8, 163-179.
1600
 */
1601
1602
0
#define a0  -0.5
1603
0
#define a1   0.3333333
1604
0
#define a2  -0.2500068
1605
0
#define a3   0.2000118
1606
0
#define a4  -0.1661269
1607
0
#define a5   0.1421878
1608
0
#define a6  -0.1384794
1609
0
#define a7   0.1250060
1610
1611
0
#define one_7   0.1428571428571428571
1612
0
#define one_12  0.0833333333333333333
1613
0
#define one_24  0.0416666666666666667
1614
1615
0
#define repeat for(;;)
1616
1617
0
#define M_1_SQRT_2PI    0.398942280401432677939946059934     /* 1/sqrt(2pi) */
1618
1619
0
static double igraph_i_rpois(igraph_rng_t *rng, double mu) {
1620
    /* Factorial Table (0:9)! */
1621
0
    const double fact[10] = {
1622
0
        1., 1., 2., 6., 24., 120., 720., 5040., 40320., 362880.
1623
0
    };
1624
1625
    /* These are static --- persistent between calls for same mu : */
1626
0
    static IGRAPH_THREAD_LOCAL int l;
1627
0
    static IGRAPH_THREAD_LOCAL igraph_integer_t m;
1628
1629
0
    static IGRAPH_THREAD_LOCAL double b1, b2, c, c0, c1, c2, c3;
1630
0
    static IGRAPH_THREAD_LOCAL double pp[36], p0, p, q, s, d, omega;
1631
0
    static IGRAPH_THREAD_LOCAL double big_l;/* integer "w/o overflow" */
1632
0
    static IGRAPH_THREAD_LOCAL double muprev = 0., muprev2 = 0.;/*, muold    = 0.*/
1633
1634
    /* Local Vars  [initialize some for -Wall]: */
1635
0
    double del, difmuk = 0., E = 0., fk = 0., fx, fy, g, px, py, t, u = 0., v, x;
1636
0
    double pois = -1.;
1637
0
    int k, big_mu;
1638
0
    bool new_big_mu = false;
1639
0
    bool kflag;
1640
1641
0
    if (!isfinite(mu) || mu < 0) {
1642
0
        ML_ERR_return_NAN;
1643
0
    }
1644
1645
0
    if (mu <= 0.) {
1646
0
        return 0.;
1647
0
    }
1648
1649
0
    big_mu = mu >= 10.;
1650
0
    if (big_mu) {
1651
0
        new_big_mu = false;
1652
0
    }
1653
1654
0
    if (!(big_mu && mu == muprev)) {/* maybe compute new persistent par.s */
1655
1656
0
        if (big_mu) {
1657
0
            new_big_mu = true;
1658
            /* Case A. (recalculation of s,d,l  because mu has changed):
1659
             * The Poisson probabilities pk exceed the discrete normal
1660
             * probabilities fk whenever k >= m(mu).
1661
             */
1662
0
            muprev = mu;
1663
0
            s = sqrt(mu);
1664
0
            d = 6. * mu * mu;
1665
0
            big_l = floor(mu - 1.1484);
1666
            /* = an upper bound to m(mu) for all mu >= 10.*/
1667
0
        } else { /* Small mu ( < 10) -- not using normal approx. */
1668
1669
            /* Case B. (start new table and calculate p0 if necessary) */
1670
1671
            /*muprev = 0.;-* such that next time, mu != muprev ..*/
1672
0
            if (mu != muprev) {
1673
0
                muprev = mu;
1674
0
                m = imax2(1, (igraph_integer_t) mu);
1675
0
                l = 0; /* pp[] is already ok up to pp[l] */
1676
0
                q = p0 = p = exp(-mu);
1677
0
            }
1678
1679
0
            repeat {
1680
                /* Step U. uniform sample for inversion method */
1681
0
                u = igraph_rng_get_unif01(rng);
1682
0
                if (u <= p0) {
1683
0
                    return 0.;
1684
0
                }
1685
1686
                /* Step T. table comparison until the end pp[l] of the
1687
                   pp-table of cumulative Poisson probabilities
1688
                   (0.458 > ~= pp[9](= 0.45792971447) for mu=10 ) */
1689
0
                if (l != 0) {
1690
0
                    for (k = (u <= 0.458) ? 1 : imin2(l, m);  k <= l; k++)
1691
0
                        if (u <= pp[k]) {
1692
0
                            return (double)k;
1693
0
                        }
1694
0
                    if (l == 35) { /* u > pp[35] */
1695
0
                        continue;
1696
0
                    }
1697
0
                }
1698
                /* Step C. creation of new Poisson
1699
                   probabilities p[l..] and their cumulatives q =: pp[k] */
1700
0
                l++;
1701
0
                for (k = l; k <= 35; k++) {
1702
0
                    p *= mu / k;
1703
0
                    q += p;
1704
0
                    pp[k] = q;
1705
0
                    if (u <= q) {
1706
0
                        l = k;
1707
0
                        return (double)k;
1708
0
                    }
1709
0
                }
1710
0
                l = 35;
1711
0
            } /* end(repeat) */
1712
0
        }/* mu < 10 */
1713
1714
0
    } /* end {initialize persistent vars} */
1715
1716
    /* Only if mu >= 10 : ----------------------- */
1717
1718
    /* Step N. normal sample */
1719
0
    g = mu + s * igraph_i_norm_rand(rng);/* norm_rand() ~ N(0,1), standard normal */
1720
1721
0
    if (g >= 0.) {
1722
0
        pois = floor(g);
1723
        /* Step I. immediate acceptance if pois is large enough */
1724
0
        if (pois >= big_l) {
1725
0
            return pois;
1726
0
        }
1727
        /* Step S. squeeze acceptance */
1728
0
        fk = pois;
1729
0
        difmuk = mu - fk;
1730
0
        u = igraph_rng_get_unif01(rng); /* ~ U(0,1) - sample */
1731
0
        if (d * u >= difmuk * difmuk * difmuk) {
1732
0
            return pois;
1733
0
        }
1734
0
    }
1735
1736
    /* Step P. preparations for steps Q and H.
1737
       (recalculations of parameters if necessary) */
1738
1739
0
    if (new_big_mu || mu != muprev2) {
1740
        /* Careful! muprev2 is not always == muprev
1741
        because one might have exited in step I or S
1742
        */
1743
0
        muprev2 = mu;
1744
0
        omega = M_1_SQRT_2PI / s;
1745
        /* The quantities b1, b2, c3, c2, c1, c0 are for the Hermite
1746
         * approximations to the discrete normal probabilities fk. */
1747
1748
0
        b1 = one_24 / mu;
1749
0
        b2 = 0.3 * b1 * b1;
1750
0
        c3 = one_7 * b1 * b2;
1751
0
        c2 = b2 - 15. * c3;
1752
0
        c1 = b1 - 6. * b2 + 45. * c3;
1753
0
        c0 = 1. - b1 + 3. * b2 - 15. * c3;
1754
0
        c = 0.1069 / mu; /* guarantees majorization by the 'hat'-function. */
1755
0
    }
1756
1757
0
    if (g >= 0.) {
1758
        /* 'Subroutine' F is called (kflag=0 for correct return) */
1759
0
        kflag = false;
1760
0
        goto Step_F;
1761
0
    }
1762
1763
1764
0
    repeat {
1765
        /* Step E. Exponential Sample */
1766
1767
0
        E = igraph_i_exp_rand(rng);/* ~ Exp(1) (standard exponential) */
1768
1769
        /*  sample t from the laplace 'hat'
1770
            (if t <= -0.6744 then pk < fk for all mu >= 10.) */
1771
0
        u = 2 * igraph_rng_get_unif01(rng) - 1.;
1772
0
        t = 1.8 + copysign(E, u);
1773
0
        if (t > -0.6744) {
1774
0
            pois = floor(mu + s * t);
1775
0
            fk = pois;
1776
0
            difmuk = mu - fk;
1777
1778
            /* 'subroutine' F is called (kflag=1 for correct return) */
1779
0
            kflag = true;
1780
1781
0
Step_F: /* 'subroutine' F : calculation of px,py,fx,fy. */
1782
1783
0
            if (pois < 10) { /* use factorials from table fact[] */
1784
0
                px = -mu;
1785
0
                py = pow(mu, pois) / fact[(int)pois];
1786
0
            } else {
1787
                /* Case pois >= 10 uses polynomial approximation
1788
                   a0-a7 for accuracy when advisable */
1789
0
                del = one_12 / fk;
1790
0
                del = del * (1. - 4.8 * del * del);
1791
0
                v = difmuk / fk;
1792
0
                if (fabs(v) <= 0.25)
1793
0
                    px = fk * v * v * (((((((a7 * v + a6) * v + a5) * v + a4) *
1794
0
                                          v + a3) * v + a2) * v + a1) * v + a0)
1795
0
                    - del;
1796
0
                else { /* |v| > 1/4 */
1797
0
                    px = fk * log(1. + v) - difmuk - del;
1798
0
                }
1799
0
                py = M_1_SQRT_2PI / sqrt(fk);
1800
0
            }
1801
0
            x = (0.5 - difmuk) / s;
1802
0
            x *= x;/* x^2 */
1803
0
            fx = -0.5 * x;
1804
0
            fy = omega * (((c3 * x + c2) * x + c1) * x + c0);
1805
0
            if (kflag) {
1806
                /* Step H. Hat acceptance (E is repeated on rejection) */
1807
0
                if (c * fabs(u) <= py * exp(px + E) - fy * exp(fx + E)) {
1808
0
                    break;
1809
0
                }
1810
0
            } else
1811
                /* Step Q. Quotient acceptance (rare case) */
1812
0
                if (fy - u * fy <= py * exp(px - fx)) {
1813
0
                    break;
1814
0
                }
1815
0
        }/* t > -.67.. */
1816
0
    }
1817
0
    return pois;
1818
0
}
1819
1820
#undef a1
1821
#undef a2
1822
#undef a3
1823
#undef a4
1824
#undef a5
1825
#undef a6
1826
#undef a7
1827
1828
/* This is from nmath/rbinom.c */
1829
1830
0
#define repeat for(;;)
1831
1832
0
static double igraph_i_rbinom(igraph_rng_t *rng, igraph_integer_t n, double pp) {
1833
1834
0
    static IGRAPH_THREAD_LOCAL double c, fm, npq, p1, p2, p3, p4, qn;
1835
0
    static IGRAPH_THREAD_LOCAL double xl, xll, xlr, xm, xr;
1836
1837
0
    static IGRAPH_THREAD_LOCAL double psave = -1.0;
1838
0
    static IGRAPH_THREAD_LOCAL igraph_integer_t nsave = -1;
1839
0
    static IGRAPH_THREAD_LOCAL igraph_integer_t m;
1840
1841
0
    double f, f1, f2, u, v, w, w2, x, x1, x2, z, z2;
1842
0
    double p, q, np, g, r, al, alv, amaxp, ffm, ynorm;
1843
0
    igraph_integer_t i, ix, k;
1844
1845
0
    if (!isfinite(pp) ||
1846
        /* n=0, p=0, p=1 are not errors <TSL>*/
1847
0
        n < 0 || pp < 0. || pp > 1.) {
1848
0
        ML_ERR_return_NAN;
1849
0
    }
1850
1851
0
    if (n == 0 || pp == 0.) {
1852
0
        return 0;
1853
0
    }
1854
0
    if (pp == 1.) {
1855
0
        return n;
1856
0
    }
1857
1858
0
    p = fmin(pp, 1. - pp);
1859
0
    q = 1. - p;
1860
0
    np = n * p;
1861
0
    r = p / q;
1862
0
    g = r * (n + 1);
1863
1864
    /* Setup, perform only when parameters change [using static (globals): */
1865
1866
    /* FIXING: Want this thread safe
1867
       -- use as little (thread globals) as possible
1868
    */
1869
0
    if (pp != psave || n != nsave) {
1870
0
        psave = pp;
1871
0
        nsave = n;
1872
0
        if (np < 30.0) {
1873
            /* inverse cdf logic for mean less than 30 */
1874
0
            qn = pow(q, (double) n);
1875
0
            goto L_np_small;
1876
0
        } else {
1877
0
            ffm = np + p;
1878
0
            m = ffm;
1879
0
            fm = m;
1880
0
            npq = np * q;
1881
            /* Note (igraph): Original code used a cast to (int) for rounding. However,
1882
             * the max npq = n*p*(1-p) value is 0.25*n, thus 2.195 * sqrt(npq) may be
1883
             * as large as 1.0975 * sqrt(n). This is not representable on a 32-bit signed
1884
             * integer when n is a 64-bit signed integer. Thus we use trunc() instead. */
1885
0
            p1 = trunc(2.195 * sqrt(npq) - 4.6 * q) + 0.5;
1886
0
            xm = fm + 0.5;
1887
0
            xl = xm - p1;
1888
0
            xr = xm + p1;
1889
0
            c = 0.134 + 20.5 / (15.3 + fm);
1890
0
            al = (ffm - xl) / (ffm - xl * p);
1891
0
            xll = al * (1.0 + 0.5 * al);
1892
0
            al = (xr - ffm) / (xr * q);
1893
0
            xlr = al * (1.0 + 0.5 * al);
1894
0
            p2 = p1 * (1.0 + c + c);
1895
0
            p3 = p2 + c / xll;
1896
0
            p4 = p3 + c / xlr;
1897
0
        }
1898
0
    } else if (n == nsave) {
1899
0
        if (np < 30.0) {
1900
0
            goto L_np_small;
1901
0
        }
1902
0
    }
1903
1904
    /*-------------------------- np = n*p >= 30 : ------------------- */
1905
0
    repeat {
1906
0
        u = igraph_rng_get_unif01(rng) * p4;
1907
0
        v = igraph_rng_get_unif01(rng);
1908
        /* triangular region */
1909
0
        if (u <= p1) {
1910
0
            ix = xm - p1 * v + u;
1911
0
            goto finis;
1912
0
        }
1913
        /* parallelogram region */
1914
0
        if (u <= p2) {
1915
0
            x = xl + (u - p1) / c;
1916
0
            v = v * c + 1.0 - fabs(xm - x) / p1;
1917
0
            if (v > 1.0 || v <= 0.) {
1918
0
                continue;
1919
0
            }
1920
0
            ix = x;
1921
0
        } else {
1922
0
            if (u > p3) { /* right tail */
1923
0
                ix = xr - log(v) / xlr;
1924
0
                if (ix > n) {
1925
0
                    continue;
1926
0
                }
1927
0
                v = v * (u - p3) * xlr;
1928
0
            } else {/* left tail */
1929
0
                ix = xl + log(v) / xll;
1930
0
                if (ix < 0) {
1931
0
                    continue;
1932
0
                }
1933
0
                v = v * (u - p2) * xll;
1934
0
            }
1935
0
        }
1936
        /* determine appropriate way to perform accept/reject test */
1937
0
        k = imaxabs(ix - m);
1938
0
        if (k <= 20 || k >= npq / 2 - 1) {
1939
            /* explicit evaluation */
1940
0
            f = 1.0;
1941
0
            if (m < ix) {
1942
0
                for (i = m + 1; i <= ix; i++) {
1943
0
                    f *= (g / i - r);
1944
0
                }
1945
0
            } else if (m != ix) {
1946
0
                for (i = ix + 1; i <= m; i++) {
1947
0
                    f /= (g / i - r);
1948
0
                }
1949
0
            }
1950
0
            if (v <= f) {
1951
0
                goto finis;
1952
0
            }
1953
0
        } else {
1954
            /* squeezing using upper and lower bounds on log(f(x)) */
1955
0
            amaxp = (k / npq) * ((k * (k / 3. + 0.625) + 0.1666666666666) / npq + 0.5);
1956
0
            ynorm = -k * k / (2.0 * npq);
1957
0
            alv = log(v);
1958
0
            if (alv < ynorm - amaxp) {
1959
0
                goto finis;
1960
0
            }
1961
0
            if (alv <= ynorm + amaxp) {
1962
                /* Stirling's formula to machine accuracy */
1963
                /* for the final acceptance/rejection test */
1964
0
                x1 = ix + 1;
1965
0
                f1 = fm + 1.0;
1966
0
                z = n + 1 - fm;
1967
0
                w = n - ix + 1.0;
1968
0
                z2 = z * z;
1969
0
                x2 = x1 * x1;
1970
0
                f2 = f1 * f1;
1971
0
                w2 = w * w;
1972
0
                if (alv <= xm * log(f1 / x1) + (n - m + 0.5) * log(z / w) + (ix - m) * log(w * p / (x1 * q)) + (13860.0 - (462.0 - (132.0 - (99.0 - 140.0 / f2) / f2) / f2) / f2) / f1 / 166320.0 + (13860.0 - (462.0 - (132.0 - (99.0 - 140.0 / z2) / z2) / z2) / z2) / z / 166320.0 + (13860.0 - (462.0 - (132.0 - (99.0 - 140.0 / x2) / x2) / x2) / x2) / x1 / 166320.0 + (13860.0 - (462.0 - (132.0 - (99.0 - 140.0 / w2) / w2) / w2) / w2) / w / 166320.) {
1973
0
                    goto finis;
1974
0
                }
1975
0
            }
1976
0
        }
1977
0
    }
1978
1979
0
L_np_small:
1980
    /*---------------------- np = n*p < 30 : ------------------------- */
1981
1982
0
    repeat {
1983
0
        ix = 0;
1984
0
        f = qn;
1985
0
        u = igraph_rng_get_unif01(rng);
1986
0
        repeat {
1987
0
            if (u < f) {
1988
0
                goto finis;
1989
0
            }
1990
0
            if (ix > 110) {
1991
0
                break;
1992
0
            }
1993
0
            u -= f;
1994
0
            ix++;
1995
0
            f *= (g / ix - r);
1996
0
        }
1997
0
    }
1998
0
finis:
1999
0
    if (psave > 0.5) {
2000
0
        ix = n - ix;
2001
0
    }
2002
0
    return (double)ix;
2003
0
}
2004
2005
0
static igraph_real_t igraph_i_rexp(igraph_rng_t *rng, double rate) {
2006
0
    igraph_real_t scale = 1.0 / rate;
2007
0
    if (!isfinite(scale) || scale <= 0.0) {
2008
0
        if (scale == 0.0) {
2009
0
            return 0.0;
2010
0
        }
2011
0
        return IGRAPH_NAN;
2012
0
    }
2013
0
    return scale * igraph_i_exp_rand(rng);
2014
0
}
2015
2016
/* This is from nmath/rgamma.c */
2017
2018
/*
2019
 *  Mathlib : A C Library of Special Functions
2020
 *  Copyright (C) 1998 Ross Ihaka
2021
 *  Copyright (C) 2000--2008 The R Core Team
2022
 *
2023
 *  This program is free software; you can redistribute it and/or modify
2024
 *  it under the terms of the GNU General Public License as published by
2025
 *  the Free Software Foundation; either version 2 of the License, or
2026
 *  (at your option) any later version.
2027
 *
2028
 *  This program is distributed in the hope that it will be useful,
2029
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
2030
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2031
 *  GNU General Public License for more details.
2032
 *
2033
 *  You should have received a copy of the GNU General Public License
2034
 *  along with this program; if not, a copy is available at
2035
 *  http://www.r-project.org/Licenses/
2036
 *
2037
 *  SYNOPSIS
2038
 *
2039
 *    #include <Rmath.h>
2040
 *    double rgamma(double a, double scale);
2041
 *
2042
 *  DESCRIPTION
2043
 *
2044
 *    Random variates from the gamma distribution.
2045
 *
2046
 *  REFERENCES
2047
 *
2048
 *    [1] Shape parameter a >= 1.  Algorithm GD in:
2049
 *
2050
 *    Ahrens, J.H. and Dieter, U. (1982).
2051
 *    Generating gamma variates by a modified
2052
 *    rejection technique.
2053
 *    Comm. ACM, 25, 47-54.
2054
 *
2055
 *
2056
 *    [2] Shape parameter 0 < a < 1. Algorithm GS in:
2057
 *
2058
 *    Ahrens, J.H. and Dieter, U. (1974).
2059
 *    Computer methods for sampling from gamma, beta,
2060
 *    poisson and binomial distributions.
2061
 *    Computing, 12, 223-246.
2062
 *
2063
 *    Input: a = parameter (mean) of the standard gamma distribution.
2064
 *    Output: a variate from the gamma(a)-distribution
2065
 */
2066
2067
0
static double igraph_i_rgamma(igraph_rng_t *rng, double a, double scale) {
2068
    /* Constants : */
2069
0
    static const double sqrt32 = 5.656854;
2070
0
    static const double exp_m1 = 0.36787944117144232159;/* exp(-1) = 1/e */
2071
2072
    /* Coefficients q[k] - for q0 = sum(q[k]*a^(-k))
2073
     * Coefficients a[k] - for q = q0+(t*t/2)*sum(a[k]*v^k)
2074
     * Coefficients e[k] - for exp(q)-1 = sum(e[k]*q^k)
2075
     */
2076
0
    static const double q1 = 0.04166669;
2077
0
    static const double q2 = 0.02083148;
2078
0
    static const double q3 = 0.00801191;
2079
0
    static const double q4 = 0.00144121;
2080
0
    static const double q5 = -7.388e-5;
2081
0
    static const double q6 = 2.4511e-4;
2082
0
    static const double q7 = 2.424e-4;
2083
2084
0
    static const double a1 = 0.3333333;
2085
0
    static const double a2 = -0.250003;
2086
0
    static const double a3 = 0.2000062;
2087
0
    static const double a4 = -0.1662921;
2088
0
    static const double a5 = 0.1423657;
2089
0
    static const double a6 = -0.1367177;
2090
0
    static const double a7 = 0.1233795;
2091
2092
    /* State variables: */
2093
0
    static IGRAPH_THREAD_LOCAL double aa = 0.;
2094
0
    static IGRAPH_THREAD_LOCAL double aaa = 0.;
2095
0
    static IGRAPH_THREAD_LOCAL double s, s2, d;    /* no. 1 (step 1) */
2096
0
    static IGRAPH_THREAD_LOCAL double q0, b, si, c;/* no. 2 (step 4) */
2097
2098
0
    double e, p, q, r, t, u, v, w, x, ret_val;
2099
2100
0
    if (!isfinite(a) || !isfinite(scale) || a < 0.0 || scale <= 0.0) {
2101
0
        if (scale == 0.) {
2102
0
            return 0.;
2103
0
        }
2104
0
        ML_ERR_return_NAN;
2105
0
    }
2106
2107
0
    if (a < 1.) { /* GS algorithm for parameters a < 1 */
2108
0
        if (a == 0) {
2109
0
            return 0.;
2110
0
        }
2111
0
        e = 1.0 + exp_m1 * a;
2112
0
        repeat {
2113
0
            p = e * igraph_rng_get_unif01(rng);
2114
0
            if (p >= 1.0) {
2115
0
                x = -log((e - p) / a);
2116
0
                if (igraph_i_exp_rand(rng) >= (1.0 - a) * log(x)) {
2117
0
                    break;
2118
0
                }
2119
0
            } else {
2120
0
                x = exp(log(p) / a);
2121
0
                if (igraph_i_exp_rand(rng) >= x) {
2122
0
                    break;
2123
0
                }
2124
0
            }
2125
0
        }
2126
0
        return scale * x;
2127
0
    }
2128
2129
    /* --- a >= 1 : GD algorithm --- */
2130
2131
    /* Step 1: Recalculations of s2, s, d if a has changed */
2132
0
    if (a != aa) {
2133
0
        aa = a;
2134
0
        s2 = a - 0.5;
2135
0
        s = sqrt(s2);
2136
0
        d = sqrt32 - s * 12.0;
2137
0
    }
2138
    /* Step 2: t = standard normal deviate,
2139
               x = (s,1/2) -normal deviate. */
2140
2141
    /* immediate acceptance (i) */
2142
0
    t = igraph_i_norm_rand(rng);
2143
0
    x = s + 0.5 * t;
2144
0
    ret_val = x * x;
2145
0
    if (t >= 0.0) {
2146
0
        return scale * ret_val;
2147
0
    }
2148
2149
    /* Step 3: u = 0,1 - uniform sample. squeeze acceptance (s) */
2150
0
    u = igraph_rng_get_unif01(rng);
2151
0
    if (d * u <= t * t * t) {
2152
0
        return scale * ret_val;
2153
0
    }
2154
2155
    /* Step 4: recalculations of q0, b, si, c if necessary */
2156
2157
0
    if (a != aaa) {
2158
0
        aaa = a;
2159
0
        r = 1.0 / a;
2160
0
        q0 = ((((((q7 * r + q6) * r + q5) * r + q4) * r + q3) * r
2161
0
               + q2) * r + q1) * r;
2162
2163
        /* Approximation depending on size of parameter a */
2164
        /* The constants in the expressions for b, si and c */
2165
        /* were established by numerical experiments */
2166
2167
0
        if (a <= 3.686) {
2168
0
            b = 0.463 + s + 0.178 * s2;
2169
0
            si = 1.235;
2170
0
            c = 0.195 / s - 0.079 + 0.16 * s;
2171
0
        } else if (a <= 13.022) {
2172
0
            b = 1.654 + 0.0076 * s2;
2173
0
            si = 1.68 / s + 0.275;
2174
0
            c = 0.062 / s + 0.024;
2175
0
        } else {
2176
0
            b = 1.77;
2177
0
            si = 0.75;
2178
0
            c = 0.1515 / s;
2179
0
        }
2180
0
    }
2181
    /* Step 5: no quotient test if x not positive */
2182
2183
0
    if (x > 0.0) {
2184
        /* Step 6: calculation of v and quotient q */
2185
0
        v = t / (s + s);
2186
0
        if (fabs(v) <= 0.25)
2187
0
            q = q0 + 0.5 * t * t * ((((((a7 * v + a6) * v + a5) * v + a4) * v
2188
0
                                      + a3) * v + a2) * v + a1) * v;
2189
0
        else {
2190
0
            q = q0 - s * t + 0.25 * t * t + (s2 + s2) * log(1.0 + v);
2191
0
        }
2192
2193
2194
        /* Step 7: quotient acceptance (q) */
2195
0
        if (log(1.0 - u) <= q) {
2196
0
            return scale * ret_val;
2197
0
        }
2198
0
    }
2199
2200
0
    repeat {
2201
        /* Step 8: e = standard exponential deviate
2202
         *  u =  0,1 -uniform deviate
2203
         *  t = (b,si)-double exponential (laplace) sample */
2204
0
        e = igraph_i_exp_rand(rng);
2205
0
        u = igraph_rng_get_unif01(rng);
2206
0
        u = u + u - 1.0;
2207
0
        if (u < 0.0) {
2208
0
            t = b - si * e;
2209
0
        } else {
2210
0
            t = b + si * e;
2211
0
        }
2212
        /* Step  9:  rejection if t < tau(1) = -0.71874483771719 */
2213
0
        if (t >= -0.71874483771719) {
2214
            /* Step 10:  calculation of v and quotient q */
2215
0
            v = t / (s + s);
2216
0
            if (fabs(v) <= 0.25)
2217
0
                q = q0 + 0.5 * t * t *
2218
0
                ((((((a7 * v + a6) * v + a5) * v + a4) * v + a3) * v
2219
0
                  + a2) * v + a1) * v;
2220
0
            else {
2221
0
                q = q0 - s * t + 0.25 * t * t + (s2 + s2) * log(1.0 + v);
2222
0
            }
2223
            /* Step 11:  hat acceptance (h) */
2224
            /* (if q not positive go to step 8) */
2225
0
            if (q > 0.0) {
2226
0
                w = expm1(q);
2227
                /*  ^^^^^ original code had approximation with rel.err < 2e-7 */
2228
                /* if t is rejected sample again at step 8 */
2229
0
                if (c * fabs(u) <= w * exp(e - 0.5 * t * t)) {
2230
0
                    break;
2231
0
                }
2232
0
            }
2233
0
        }
2234
0
    } /* repeat .. until  `t' is accepted */
2235
0
    x = s + 0.5 * t;
2236
0
    return scale * x * x;
2237
0
}