DeSiGNAR  0.5a
Data Structures General Library
random.H
Go to the documentation of this file.
1 /*
2  This file is part of Designar.
3  Copyright (C) 2017 by Alejandro J. Mujica
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Any user request of this software, write to
19 
20  Alejandro Mujica
21 
22  aledrums@gmail.com
23 */
24 
25 # ifndef DSGRANDOM_H
26 # define DSGRANDOM_H
27 
28 # include <types.H>
29 
30 namespace Designar
31 {
32 
33  constexpr lint_t NUM_BITS = 64;
34  constexpr real_t DEFAULT_P = 0.5;
36 
38 
39  real_t random(rng_t &);
40 
41  template <typename T>
42  T random_uniform(rng_t &, T);
43 
44  template <typename T>
45  T random_uniform(rng_t &, T, T);
46 
47  bool random_Bernoulli(rng_t &, real_t p = DEFAULT_P);
48 
49  nat_t random_binomial(rng_t &, nat_t, real_t p = DEFAULT_P);
50 
51  nat_t throw_dice(rng_t &, nat_t num_faces = DEFAULT_DICE_NUM_FACES);
52 
53  template <typename T>
54  T random_uniform(rng_t & rng, T max)
55  {
56  static_assert(std::is_arithmetic<T>::value,
57  "Template argument must be an arithmetic type");
58 
59  return random(rng) * max;
60  }
61 
62  template <typename T>
63  T random_uniform(rng_t & rng, T l, T r)
64  {
65  static_assert(std::is_arithmetic<T>::value,
66  "Template argument must be an arithmetic type");
67 
68  return random_uniform<T>(rng, r - l) + l;
69  }
70 
71 } // end namespace Designar
72 
73 # endif // DSGRANDOM_H
nat_t throw_dice(rng_t &, nat_t num_faces=DEFAULT_DICE_NUM_FACES)
T random_uniform(rng_t &, T)
Definition: random.H:54
constexpr lint_t NUM_BITS
Definition: random.H:33
double real_t
Definition: types.H:51
std::mt19937_64 rng_t
Definition: types.H:52
nat_t random_binomial(rng_t &, nat_t, real_t p=DEFAULT_P)
real_t random(rng_t &)
rng_t::result_type rng_seed_t
Definition: types.H:53
rng_seed_t get_random_seed()
long long lint_t
Definition: types.H:49
constexpr nat_t DEFAULT_DICE_NUM_FACES
Definition: random.H:35
Definition: array.H:32
unsigned long int nat_t
Definition: types.H:50
Value & value(MapKey< Key, Value > &item)
Definition: map.H:77
constexpr real_t DEFAULT_P
Definition: random.H:34
bool random_Bernoulli(rng_t &, real_t p=DEFAULT_P)