C++ Mathematical Expression Toolkit (ExprTk)
release
Toggle main menu visibility
Loading...
Searching...
No Matches
exprtk
exprtk_normal_random_marsaglia_method.cpp
Go to the documentation of this file.
1
/*
2
**************************************************************
3
* C++ Mathematical Expression Toolkit Library *
4
* *
5
* ExprTk Normal Random Variables Via Marsaglia Method *
6
* Author: Arash Partow (1999-2025) *
7
* URL: https://www.partow.net/programming/exprtk/index.html *
8
* *
9
* Copyright notice: *
10
* Free use of the Mathematical Expression Toolkit Library is *
11
* permitted under the guidelines and in accordance with the *
12
* most current version of the MIT License. *
13
* https://www.opensource.org/licenses/MIT *
14
* SPDX-License-Identifier: MIT *
15
* *
16
**************************************************************
17
*/
18
19
20
#include <algorithm>
21
#include <array>
22
#include <cstdio>
23
#include <random>
24
#include <string>
25
26
#include "
exprtk.hpp
"
27
28
29
template
<
typename
T>
30
struct
uniform_random_01
final :
public
exprtk::ifunction
<T>
31
{
32
using
exprtk::ifunction
<T>::operator();
33
34
uniform_random_01
()
35
:
exprtk
::
ifunction
<T>(0)
36
{
37
std::random_device device;
38
std::array<unsigned int,std::mt19937::state_size> seed;
39
std::generate_n(seed.data(), seed.size(), std::ref(device));
40
std::seed_seq seq(std::begin(seed), std::end(seed));
41
generator
.seed(seq);
42
}
43
44
inline
T
operator()
()
override
45
{
46
return
distribution
(
generator
);
47
}
48
49
std::mt19937
generator
;
50
std::uniform_real_distribution<T>
distribution
{ T(0), T(1) };
51
};
52
53
template
<
typename
T>
54
void
normal_random_marsaglia_method
()
55
{
56
typedef
exprtk::symbol_table<T>
symbol_table_t;
57
typedef
exprtk::expression<T>
expression_t;
58
typedef
exprtk::parser<T>
parser_t;
59
typedef
exprtk::function_compositor<T>
compositor_t;
60
typedef
typename
compositor_t::function function_t;
61
62
exprtk::rtl::io::println<T>
println;
63
uniform_random_01<T>
unirandom;
64
65
symbol_table_t symbol_table;
66
symbol_table.add_function(
"println"
, println );
67
symbol_table.add_function(
"random"
, unirandom);
68
symbol_table.add_constants();
69
70
compositor_t compositor(symbol_table);
71
72
compositor.add(
73
function_t(
"normal_distribution"
)
74
.vars(
"mean"
,
"stddev"
)
75
.expression
76
(
77
" var u := 0; "
78
" var v := 0; "
79
" var s := 0; "
80
" "
81
" repeat "
82
" u := random() * 2 - 1; "
83
" v := random() * 2 - 1; "
84
" s := u^2 + v^2; "
85
" until (s > 0 and s < 1); "
86
" "
87
" s := sqrt(-2 * log(s) / s); "
88
" mean + stddev * u * s; "
89
));
90
91
const
std::string normal_random_marsaglia_method_program =
92
" const var mean := pi; "
93
" const var stddev := 3 * mean / 2; "
94
" const var num_samples := 1e7; "
95
" var values[num_samples] := [normal_distribution(mean, stddev)]; "
96
" "
97
" var sample_mean := avg(values); "
98
" var sample_stddev := sqrt(sum([values - sample_mean]^2) / values[]); "
99
" "
100
" println('sample mean: ', sample_mean , ' error: ', abs(sample_mean - mean)); "
101
" println('sample stddev: ', sample_stddev, ' error: ', abs(sample_stddev - stddev)); "
102
" "
103
" "
;
104
105
expression_t expression;
106
expression.register_symbol_table(symbol_table);
107
108
parser_t parser;
109
parser.compile(normal_random_marsaglia_method_program,expression);
110
111
expression.value();
112
}
113
114
int
main
()
115
{
116
normal_random_marsaglia_method<double>
();
117
return
0;
118
}
exprtk::expression
Definition
exprtk.hpp:21832
exprtk::function_compositor
Definition
exprtk.hpp:43057
exprtk::ifunction
Definition
exprtk.hpp:19861
exprtk::ifunction::ifunction
ifunction(const std::size_t &pc)
Definition
exprtk.hpp:19864
exprtk::parser
Definition
exprtk.hpp:22525
exprtk::symbol_table
Definition
exprtk.hpp:20090
exprtk.hpp
normal_random_marsaglia_method
void normal_random_marsaglia_method()
Definition
exprtk_normal_random_marsaglia_method.cpp:54
main
int main()
Definition
exprtk_normal_random_marsaglia_method.cpp:114
exprtk
Definition
exprtk.hpp:60
exprtk::rtl::io::println
Definition
exprtk.hpp:44273
uniform_random_01
Definition
exprtk_normal_random_marsaglia_method.cpp:31
uniform_random_01::operator()
T operator()() override
Definition
exprtk_normal_random_marsaglia_method.cpp:44
uniform_random_01::distribution
std::uniform_real_distribution< T > distribution
Definition
exprtk_normal_random_marsaglia_method.cpp:50
uniform_random_01::uniform_random_01
uniform_random_01()
Definition
exprtk_normal_random_marsaglia_method.cpp:34
uniform_random_01::generator
std::mt19937 generator
Definition
exprtk_normal_random_marsaglia_method.cpp:49
Generated by
1.17.0