biomcmc-lib  0.1
low level library for phylogenetic analysis
random_number_gen.h
1 /*
2  * This file is part of biomcmc-lib, a low-level library for phylogenomic analysis.
3  * Copyright (C) 2019-today Leonardo de Oliveira Martins [ leomrtns at gmail.com; http://www.leomartins.org ]
4  *
5  * biomcmc is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
6  * License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * This program is distributed in the hope that it will be usefulULL, but WITHOUT ANY WARRANTY; without even the implied
10  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULLAR PURPOSE. See the GNU General Public License for more
11  * details (file "COPYING" or http://www.gnu.org/copyleft/gpl.html).
12  */
13 
17 #ifndef _biomcmc_random_number_gen_h_
18 #define _biomcmc_random_number_gen_h_
19 
20 #include "hashtable.h"
21 
22 /* Tausworthe linear feedback shift register PRNG */
23 /* 6 vectors (A,k,q,r,C,s) of size at most 5 (4 or 5, depending on stream) */
24 typedef struct { uint64_t x[30]; int n; } rng_taus_struct;
25 
26 /* \brief XOR-shift (R. P. Brent's xorgens) */
27 /* the vector itself has only 64 elements but the 65th one is the Weyl generator; */
28 typedef struct { uint64_t x[65]; int n; } rng_xorshift_struct;
29 
31 typedef struct { uint64_t x[312]; int n; } rng_mt19937_struct;
32 
34 typedef struct { uint32_t x[624]; int n; } rng_mt19937ar_struct;
35 
37 typedef struct { uint32_t x[16384]; int n; } rng_gfsr4_struct; /*16384 = 2^14 */
38 
40 typedef struct { uint32_t x[128]; int n; } rng_diaconis_struct;
41 
43 typedef struct { uint32_t x[25]; int n; } rng_tt800_struct;
44 
46 typedef struct { uint32_t x[256]; int n; } rng_lfib4_struct;
47 
49 typedef struct { uint32_t x[258]; int n; } rng_swb_struct;
50 
51 /* Panneton, L'Ecuyer, and Matsumoto WELLRNG1024a */
52 typedef struct { uint32_t x[32]; int n; } rng_well1024_struct;
53 
56 /* https://gcc.gnu.org/onlinedocs/gcc/Inline.html --> inline behaviour changed in GNU11 which is the dafault for GCC5 */
57 extern uint64_t rng_get_taus (rng_taus_struct *r);
58 void rng_set_taus (rng_taus_struct *r, uint64_t seed, int stream);
59 void rng_set_stream_taus (rng_taus_struct *r, int stream_number);
60 
61 extern uint64_t rng_get_xorshift (rng_xorshift_struct *r);
62 void rng_set_xorshift (rng_xorshift_struct *r, uint64_t seed);
63 
65 extern uint64_t rng_get_mt19937 (rng_mt19937_struct *r);
66 void rng_set_mt19937 (rng_mt19937_struct *r, uint64_t seed);
67 
69 extern uint32_t rng_get_mt19937ar (rng_mt19937ar_struct *r);
70 void rng_set_mt19937ar (rng_mt19937ar_struct *r, uint32_t seed);
71 
73 extern uint32_t rng_get_gfsr4 (rng_gfsr4_struct *r);
74 void rng_set_gfsr4 (rng_gfsr4_struct *r, uint32_t seed);
75 
76 uint32_t rng_get_diaconis (rng_diaconis_struct *r);
77 void rng_set_diaconis (rng_diaconis_struct *r, uint32_t seed);
78 
79 /* TT800 (period = 2^800)
80  * Makoto Matsumoto & Y. Kurita, Twisted GFSR Generators II, ACM Trans. Model. Comput. Simul., 4 (1994) 254-266 */
81 uint32_t rng_get_tt800 (rng_tt800_struct *r);
82 void rng_set_tt800 (rng_tt800_struct *r, uint32_t seed);
83 
84 /* Marsaglia's four-lag generator using addition */
85 uint32_t rng_get_lfib4 (rng_lfib4_struct *r);
86 void rng_set_lfib4 (rng_swb_struct *r, uint32_t seed);
87 
88 /* Marsaglia's subtract-with-borrow generator with long period (2^7578) but fails at birthday tests */
89 uint32_t rng_get_swb (rng_swb_struct *r);
90 void rng_set_swb (rng_swb_struct *r, uint32_t seed);
91 
92 /* Panneton, L'Ecuyer, and Matsumoto WELLRNG1024a */
93 uint32_t rng_get_well1024 (rng_well1024_struct *r);
94 void rng_set_well1024 (rng_well1024_struct *r, uint32_t seed);
95 
96 /* simple algorithms (up to four variables, no vector initialization) */
97 
98 uint32_t rng_get_gamerand (uint32_t *game);
99 void rng_set_gamerand (uint32_t *game, uint32_t seed);
100 
101 /* George Marsaglia's Two-multiply with carry generator (period ~ 2^60) */
102 uint32_t rng_get_marsaglia (uint32_t *m);
103 void rng_set_marsaglia (uint32_t *m, uint32_t seed);
104 
105 /* Multiplicative, Congruential Random-Number Generators with Multipliers +-2^k1 +- 2^k2 and Modulus 2^p - 1,
106  ACM Trans. Math. Software 23 (1997) 255-265. Pei-Chi Wu. */
107 uint64_t rng_get_std61 (uint64_t *x);
108 
109 /* The Minimal Portable Random Number Generator (32 bits)
110  G. S. Fishman, L. R. Moore; An statistical exhaustive analysis of multiplicative congruential random number
111  generators with modulus 2^31-1, SIAM J. Sci. Statist. Comput., 7 (1986) 24-45. Erratum, ibid, 7 (1986) 1058 */
112 uint32_t rng_get_std31 (uint32_t *x);
113 
114 /* Marsaglia's 3-shift-register generator (period 2^32-1) */
115 uint32_t rng_get_shr (uint32_t *x);
116 
117 /* Brent auxiliary function to decrease correlation between seeds */
118 uint32_t rng_get_brent (uint32_t *x);
119 uint64_t rng_get_brent_64bits (uint64_t *x);
120 
121 /* congruential quick-and-dirty generator. */
122 uint32_t rng_get_cong (uint32_t *x);
123 uint32_t rng_get_cong_many (uint32_t *x);
124 
125 /* randomize an initialized vector following Nishimura and Matsumoto's MT19937 */
126 uint32_t rng_twist_array_32bits (uint32_t *a, uint32_t n_a, uint32_t seed);
127 uint64_t rng_twist_array_64bits (uint64_t *a, uint32_t n_a, uint64_t seed);
128 
129 /* randomize after possibly initializing a vector using XOR concatenation */
130 uint32_t rng_randomize_array_32bits (uint32_t *a, uint32_t n_a, uint32_t seed, bool first_time);
131 uint64_t rng_randomize_array_64bits (uint64_t *a, uint32_t n_a, uint64_t seed, bool first_time);
132 
133 #endif
MT19937-64, the Mersenne Twister for 64 bits.
Definition: random_number_gen.h:31
Persi Diaconis' lagged Fibonacci.
Definition: random_number_gen.h:40
Definition: random_number_gen.h:52
Marsaglia's Subtract-with-borrow generator.
Definition: random_number_gen.h:49
GFSR4 implementation from GSL.
Definition: random_number_gen.h:37
Definition: random_number_gen.h:28
Marsaglia's LFIB4 lagged Fibonacci using addition.
Definition: random_number_gen.h:46
MT19937, the original Mersenne Twister (for 32 bits); the name "ar" comes from "array".
Definition: random_number_gen.h:34
Definition: random_number_gen.h:24
double hashing open-address hash table using strings as key – also has distance matrix, that can be used in alignments and trees
tt800 (small cousin of MT19937, the Mersenne Twister)
Definition: random_number_gen.h:43