biomcmc-lib  0.1
low level library for phylogenetic analysis
random_number.h
Go to the documentation of this file.
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 useful, 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 
63 #ifndef _biomcmc_random_number_h_
64 #define _biomcmc_random_number_h_
65 
66 #include "random_number_gen.h"
67 
68 typedef struct biomcmc_rng_struct* biomcmc_rng;
69 
72 {
73  rng_taus_struct taus;
76  uint64_t bit32;
77  bool have_bit32;
79  double rnorm32, rnorm64;
80  bool have_rnorm32, have_rnorm64;
81 };
82 
84 extern biomcmc_rng biomcmc_random_number; /* in OSX at least, the linker will bundle all sources into one library,
85  and thus global variables must be declared "extern" in the headers
86  and defined only once in the corresponding .c file (to avoid duplicates).
87  Functions don't need this because they are implicitly declared "extern"
88  Another solution is to declare all global variables as "static" but this
89  is dangerous since we may have several static copies. */
90 
98 void biomcmc_random_number_init (unsigned long long int seed);
101 
103 biomcmc_rng new_biomcmc_rng (unsigned long long int seed, int stream_number);
105 void del_biomcmc_rng (biomcmc_rng r);
106 
108 unsigned long long int biomcmc_rng_get_initial_seed (void);
111 biomcmc_rng new_biomcmc_rng_from_seed (unsigned long long int seed, int stream_number);
112 
114 extern double biomcmc_rng_snorm32 (void);
116 extern double biomcmc_rng_snorm (void);
117 
119 extern double biomcmc_rng_unif (void);
121 extern double biomcmc_rng_unif_pos (void);
123 extern uint64_t biomcmc_rng_unif_int64 (uint64_t n);
124 
126 extern double biomcmc_rng_unif32 (void);
128 extern double biomcmc_rng_unif_pos32 (void);
130 extern uint32_t biomcmc_rng_unif_int (uint32_t n);
131 
132 /* * * low level functions (direct access to generator) */
133 
135 extern uint64_t biomcmc_rng_get (void);
137 extern double biomcmc_rng_get_52 (void);
139 extern uint32_t biomcmc_rng_get_32 (void);
140 
142 void biomcmc_get_time (int *time);
144 double biomcmc_elapsed_time (int *now, int *past);
145 
146 #endif
MT19937-64, the Mersenne Twister for 64 bits.
Definition: random_number_gen.h:31
uint32_t biomcmc_rng_get_32(void)
new value with 32 random bits
Definition: random_number.c:217
double biomcmc_rng_get_52(void)
new value with 52 random bits as a double precision float
Definition: random_number.c:208
rng_mt19937_struct mt
Tausworthe linear feedback shift-register from GSL.
Definition: random_number.h:74
unsigned long long int biomcmc_rng_get_initial_seed(void)
Create initial seed based on time, combining time in microseconds and seconds (user-controled seed is...
Definition: random_number.c:66
double biomcmc_rng_unif_pos(void)
Returns a positive random number between 0 and 1 (excluding 0 and including 1) (52 bits)...
Definition: random_number.c:170
double rnorm32
when using 32 bits we first check if we have one stored
Definition: random_number.h:79
double biomcmc_elapsed_time(int *now, int *past)
returns the floating-point time in seconds elapsed between past[2] and now[2]
Definition: random_number.c:257
void biomcmc_random_number_init(unsigned long long int seed)
High-level setup of a simple random number generator and initialization with a seed (not to be mixed ...
Definition: random_number.c:22
void del_biomcmc_rng(biomcmc_rng r)
Release memory occupied by biomcmc_rng::
Definition: random_number.c:60
void biomcmc_random_number_finalize(void)
High-level finalization (memory release etc.) of the random number environment.
Definition: random_number.c:33
uint64_t bit32
64 bits Mersenne Twister from Matsumoto's webpage
Definition: random_number.h:76
double biomcmc_rng_snorm32(void)
Returns a random number from a Standard Normal distribution N(0,1) - prob_distribution.h has general case.
Definition: random_number.c:111
bool have_bit32
temporary values when only 32 bits are necessary
Definition: random_number.h:77
Definition: random_number_gen.h:24
bool have_rnorm32
stored standard normal random values with 32 and 52 bits of precision
Definition: random_number.h:80
double biomcmc_rng_unif(void)
Returns a random number between 0 and 1 (including 1) with precision (52 bits).
Definition: random_number.c:156
biomcmc_rng new_biomcmc_rng(unsigned long long int seed, int stream_number)
Allocate memory for new (Tausworthe + MT19937) generator from a pool of streams.
Definition: random_number.c:41
Random number structure (combined Tausworthe algorithm)
Definition: random_number.h:71
void biomcmc_get_time(int *time)
get current time with maximum precision and soter in vector time[2]
Definition: random_number.c:235
uint64_t biomcmc_rng_unif_int64(uint64_t n)
Returns a long integer (64 bits) random number between 0 and n (excluding n), with ...
Definition: random_number.c:190
double biomcmc_rng_unif32(void)
Returns a random number between 0 and 1 (including 1) with precision (32 int bits).
Definition: random_number.c:150
uint32_t biomcmc_rng_unif_int(uint32_t n)
Returns an integer (32 bits) random number between 0 and n (excluding n), provided approx...
Definition: random_number.c:178
double biomcmc_rng_snorm(void)
Returns a random number from a Standard Normal distribution with maximum (52 bits) integer precision...
Definition: random_number.c:130
double biomcmc_rng_unif_pos32(void)
Returns a positive random number between 0 and 1 (including 1) (32 int bits).
Definition: random_number.c:162
biomcmc_rng new_biomcmc_rng_from_seed(unsigned long long int seed, int stream_number)
Generate a vector of seeds (based on initial one), create and initialize stream with an element of th...
Definition: random_number.c:89
biomcmc_rng biomcmc_random_number
pointer to pseudo-random number generator (should point to real stream, even when there are several) ...
Definition: random_number.c:17
uint64_t biomcmc_rng_get(void)
new value with 64 random bits
Definition: random_number.c:202