GNU Radio Manual and C++ API Reference  3.10.7.0
The Free & Open Software Radio Ecosystem
random.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2002, 2015 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * SPDX-License-Identifier: GPL-3.0-or-later
8  *
9  */
10 
11 #ifndef INCLUDED_GR_RANDOM_H
12 #define INCLUDED_GR_RANDOM_H
13 
14 #include <gnuradio/api.h>
15 #include <gnuradio/gr_complex.h>
16 #include <gnuradio/xoroshiro128p.h>
17 
18 #include <limits>
19 #include <random>
20 
21 namespace gr {
22 
23 /*!
24  * \brief wrapper for XOROSHIRO128+ PRNG for use in std::distributions
25  * Fulfills C++ named requirements for UniformRandomBitGenerator
26  * \ingroup math_blk
27  */
29 {
30 public:
31  using result_type = uint64_t; //! \brief value type is uint64
32 
33 private:
34  result_type state[2];
35 
36 public:
37  /*!
38  * \brief minimum value
39  */
40  static constexpr result_type min() { return std::numeric_limits<result_type>::min(); }
41  /*!
42  * \brief maximum value
43  */
44  static constexpr result_type max() { return std::numeric_limits<result_type>::max(); }
45 
46  /*!
47  * \brief constructor. Expects a seed.
48  */
49  xoroshiro128p_prng(uint64_t init) { seed(init); }
50 
51 
52  /*!
53  * \brief yield a random value and advance state
54  */
56 
57  /*!
58  * \brief set new seed
59  */
60  void seed(uint64_t seed) { xoroshiro128p_seed(state, seed); }
61 };
62 /*!
63  * \brief pseudo random number generator
64  * \ingroup math_blk
65  */
67 {
68 protected:
69  long d_seed;
72 
73  xoroshiro128p_prng d_rng; // mersenne twister as random number generator
74  std::uniform_real_distribution<float>
75  d_uniform; // choose uniform distribution, default is [0,1)
76  std::uniform_int_distribution<int64_t> d_integer_dis;
77 
78 public:
79  random(uint64_t seed = 0, int64_t min_integer = 0, int64_t max_integer = 2);
80  ~random();
81 
82  /*!
83  * \brief Change the seed for the initialized number generator. seed = 0 initializes
84  * the random number generator with the system time.
85  */
86  void reseed(uint64_t seed);
87 
88  /*!
89  * set minimum and maximum for integer random number generator.
90  * Limits are [minimum, maximum)
91  * Default: [0, std::numeric_limits< IntType >::max)]
92  */
93  void set_integer_limits(int64_t minimum, int64_t maximum);
94 
95  /*!
96  * Uniform random integers in the range set by 'set_integer_limits' [min, max).
97  */
98  int64_t ran_int();
99 
100  /*!
101  * \brief Uniform random numbers in the range [0.0, 1.0)
102  */
103  float ran1();
104 
105  /*!
106  * \brief Normally distributed random numbers (Gaussian distribution with zero mean
107  * and variance 1)
108  */
109  float gasdev();
110 
111  /*!
112  * \brief Laplacian distributed random numbers with zero mean and variance 1
113  */
114  float laplacian();
115 
116  /*!
117  * \brief Rayleigh distributed random numbers (zero mean and variance 1 for the
118  * underlying Gaussian distributions)
119  */
120  float rayleigh();
121 
122  /*!
123  * \brief Exponentially distributed random numbers with values less than or equal
124  * to factor replaced with zero. The underlying exponential distribution has
125  * mean sqrt(2) and variance 2.
126  */
127  float impulse(float factor);
128 
129  /*!
130  * \brief Normally distributed random numbers with zero mean and variance 1 on real
131  * and imaginary part. This results in a Rayleigh distribution for the amplitude and
132  * an uniform distribution for the phase.
133  */
134  gr_complex rayleigh_complex();
135 };
136 
137 } /* namespace gr */
138 
139 #endif /* INCLUDED_GR_RANDOM_H */
std::uniform_real_distribution< float > d_uniform
Definition: random.h:75
static constexpr result_type min()
minimum value
Definition: random.h:40
wrapper for XOROSHIRO128+ PRNG for use in std::distributions Fulfills C++ named requirements for Unif...
Definition: random.h:28
float min(float a, float b)
float d_gauss_value
Definition: random.h:71
static void xoroshiro128p_seed(uint64_t *state, const uint64_t seed)
Seed the 128 bit state from a 64 bit seed.
Definition: xoroshiro128p.h:91
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:18
static constexpr result_type max()
maximum value
Definition: random.h:44
std::complex< float > gr_complex
Definition: gr_complex.h:15
static uint64_t xoroshiro128p_next(uint64_t *state)
generate the next random number and update the state. This is the workhorse, here! ...
Definition: xoroshiro128p.h:39
uint64_t result_type
Definition: random.h:31
GNU Radio logging wrapper.
Definition: basic_block.h:29
std::uniform_int_distribution< int64_t > d_integer_dis
Definition: random.h:76
long d_seed
Definition: random.h:69
xoroshiro128p_prng(uint64_t init)
constructor. Expects a seed.
Definition: random.h:49
bool d_gauss_stored
Definition: random.h:70
void seed(uint64_t seed)
set new seed
Definition: random.h:60
result_type operator()()
yield a random value and advance state
Definition: random.h:55
xoroshiro128p_prng d_rng
Definition: random.h:73
pseudo random number generator
Definition: random.h:66