RcdMathLib_doc
Open Source Library for Linear and Non-linear Algebra
norm_dist_rnd_generator.c
Go to the documentation of this file.
1
/*
2
* Copyright (C) 2020 Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
3
* 2020 Freie Universität Berlin
4
*
5
* This file is subject to the terms and conditions of the GNU Lesser General
6
* Public License v2.1. See the file LICENSE in the top level directory for more
7
* details.
8
*/
9
24
#include <stdlib.h>
25
#include <math.h>
26
27
#include "
norm_dist_rnd_generator.h
"
28
29
30
40
double
get_norm_distr_rand_num
(
double
mean_val,
double
std_dev_val)
41
{
42
double
u, r, theta;
43
double
x;
44
double
adj_norm_rv;
45
46
// Generate a random value of u
47
u = 0.0;
48
while
(u == 0.0) {
49
u =
get_rand_num
(0);
50
}
51
52
r = sqrt(-2.0 * log(u));
53
54
// Generate a random value of theta
55
theta = 0.0;
56
while
(theta == 0.0) {
57
theta = 2.0 *
PI
*
get_rand_num
(0);
58
}
59
60
x = r * cos(theta);
61
62
adj_norm_rv = (x * std_dev_val) + mean_val;
63
64
return
(adj_norm_rv);
65
}
66
80
double
get_rand_num
(
int
initial_seed_val)
81
{
82
const
long
a = 16807;
83
const
long
m = 2147483647;
84
const
long
q = 127773;
85
const
long
r = 2836;
86
static
long
x;
87
long
x_div_q;
88
long
x_mod_q;
89
long
new_x;
90
91
// Setting of the seed value
92
if
(initial_seed_val > 0) {
93
x = initial_seed_val;
94
return
(0.0);
95
}
96
97
x_div_q = x / q;
98
x_mod_q = x % q;
99
new_x = (a * x_mod_q) - (r * x_div_q);
100
if
(new_x > 0) {
101
x = new_x;
102
}
103
else
{
104
x = new_x + m;
105
}
106
107
return
((
double
)x / m);
108
}
get_norm_distr_rand_num
double get_norm_distr_rand_num(double mean_val, double std_dev_val)
Get a normally distributed random number by applying the Box–Muller method.
Definition:
norm_dist_rnd_generator.c:40
get_rand_num
double get_rand_num(int initial_seed_val)
Generate uniform (0.0, 1.0) random numbers by using the Linear Congruential Generator (LGC) algorithm...
Definition:
norm_dist_rnd_generator.c:80
PI
#define PI
Definition:
norm_dist_rnd_generator.h:33
norm_dist_rnd_generator.h
Generating normally distributed random numbers.
linear_algebra
utilities
norm_dist_rnd_generator.c
Generated by
1.8.16