Developer documentation
Version 3.0.3-105-gd3941f44
rng.h
Go to the documentation of this file.
1/* Copyright (c) 2008-2022 the MRtrix3 contributors.
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 *
7 * Covered Software is provided under this License on an "as is"
8 * basis, without warranty of any kind, either expressed, implied, or
9 * statutory, including, without limitation, warranties that the
10 * Covered Software is free of defects, merchantable, fit for a
11 * particular purpose or non-infringing.
12 * See the Mozilla Public License v. 2.0 for more details.
13 *
14 * For more details, see http://www.mrtrix.org/.
15 */
16
17#ifndef __math_rng_h__
18#define __math_rng_h__
19
20#include <random>
21#ifdef MRTRIX_WINDOWS
22#include <sys/time.h>
23#endif
24
25#include <mutex>
26
27#include "mrtrix.h"
28
29namespace MR
30{
31 namespace Math
32 {
33
35
43 // TODO consider switch to std::mt19937_64
44 class RNG : public std::mt19937
46 public:
47 RNG () : std::mt19937 (get_seed()) { }
48 RNG (std::mt19937::result_type seed) : std::mt19937 (seed) { }
49 RNG (const RNG&) : std::mt19937 (get_seed()) { }
50 template <typename ValueType> class Uniform;
51 template <typename ValueType> class Normal;
52 template <typename ValueType> class Integer;
53
54 static std::mt19937::result_type get_seed () {
55 static std::mutex mutex;
56 std::lock_guard<std::mutex> lock (mutex);
57 static std::mt19937::result_type current_seed = get_seed_private();
58 return current_seed++;
59 }
60
61 private:
62 static std::mt19937::result_type get_seed_private () {
63 //ENVVAR name: MRTRIX_RNG_SEED
64 //ENVVAR Set the seed used for the random number generator.
65 //ENVVAR Ordinarily, MRtrix applications will use random seeds to ensure
66 //ENVVAR repeat runs of stochastic processes are never the same.
67 //ENVVAR However, when experimenting or debugging, it may be useful to
68 //ENVVAR explicitly set the RNG seed to ensure reproducible results across
69 //ENVVAR runs. To do this, set this variable to a fixed number prior to
70 //ENVVAR running the command(s).
71 //ENVVAR
72 //ENVVAR Note that to obtain the same results
73 //ENVVAR from a multi-threaded command, you should also disable
74 //ENVVAR multi-threading (using the option ``-nthread 0`` or by
75 //ENVVAR setting the :envvar:`MRTRIX_NTHREADS` environment variable to zero).
76 //ENVVAR Multi-threading introduces randomness in the order of execution, which
77 //ENVVAR will generally also affect the reproducibility of results.
78 const char* from_env = getenv ("MRTRIX_RNG_SEED");
79 if (from_env)
80 return to<std::mt19937::result_type> (from_env);
81
82 std::random_device rd;
83 return rd();
84 }
85
86
87 };
88
89
90 template <typename ValueType>
92 public:
94 using result_type = ValueType;
95 // static const ValueType max() const { return static_cast<ValueType>(1.0); }
96 // static const ValueType min() const { return static_cast<ValueType>(0.0); }
97 std::uniform_real_distribution<ValueType> dist;
98 ValueType operator() () { return dist (rng); }
99 };
100
101 template <typename ValueType>
103 public:
105 using result_type = ValueType;
106 std::normal_distribution<ValueType> dist;
107 ValueType operator() () { return dist (rng); }
108 };
109
110 template <typename ValueType>
112 public:
113 Integer (const ValueType max) :
114 dist (0, max) { }
116 std::uniform_int_distribution<ValueType> dist;
117 ValueType operator() () { return dist (rng); }
118 };
119
120 }
121}
122
123#endif
124
Integer(const ValueType max)
Definition: rng.h:113
std::uniform_int_distribution< ValueType > dist
Definition: rng.h:116
ValueType operator()()
Definition: rng.h:117
ValueType result_type
Definition: rng.h:105
std::normal_distribution< ValueType > dist
Definition: rng.h:106
ValueType operator()()
Definition: rng.h:107
ValueType result_type
Definition: rng.h:94
std::uniform_real_distribution< ValueType > dist
Definition: rng.h:97
ValueType operator()()
Definition: rng.h:98
random number generator
Definition: rng.h:45
static std::mt19937::result_type get_seed()
Definition: rng.h:54
RNG(std::mt19937::result_type seed)
Definition: rng.h:48
RNG(const RNG &)
Definition: rng.h:49
#define NOMEMALIGN
Definition: memory.h:22
Definition: base.h:24
Definition: types.h:303