XCSF 1.4.8
XCSF learning classifier system
Loading...
Searching...
No Matches
utils.c
Go to the documentation of this file.
1/*
2 * This program is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation, either version 3 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 */
15
25#include "utils.h"
26#include <limits.h>
27#include <stdbool.h>
28#include <time.h>
29
33void
35{
36 time_t now = time(0);
37 const unsigned char *p = (unsigned char *) &now;
38 uint32_t seed = 0;
39 for (size_t i = 0; i < sizeof(now); ++i) {
40 seed = (seed * (UCHAR_MAX + 2U)) + p[i];
41 }
42 dsfmt_gv_init_gen_rand(seed);
43}
44
49void
50rand_init_seed(const uint32_t seed)
51{
52 dsfmt_gv_init_gen_rand(seed);
53}
54
61double
62rand_uniform(const double min, const double max)
63{
64 return min + (dsfmt_gv_genrand_open_open() * (max - min));
65}
66
73int
74rand_uniform_int(const int min, const int max)
75{
76 return (int) floor(rand_uniform(min, max));
77}
78
86double
87rand_normal(const double mu, const double sigma)
88{
89 static const double two_pi = 2 * M_PI;
90 static double z1;
91 static bool generate;
92 generate = !generate;
93 if (!generate) {
94 return z1 * sigma + mu;
95 }
96 const double u1 = dsfmt_gv_genrand_open_open();
97 const double u2 = dsfmt_gv_genrand_open_open();
98 const double z0 = sqrt(-2 * log(u1)) * cos(two_pi * u2);
99 z1 = sqrt(-2 * log(u1)) * sin(two_pi * u2);
100 return z0 * sigma + mu;
101}
102
108void
109utils_json_parse_check(const cJSON *json)
110{
111 if (json == NULL) {
112 printf("Error reading JSON\n");
113 const char *error_ptr = cJSON_GetErrorPtr();
114 if (error_ptr != NULL) {
115 printf("Error before: %s\n", error_ptr);
116 }
117 exit(EXIT_FAILURE);
118 }
119}
int rand_uniform_int(const int min, const int max)
Returns a uniform random integer [min,max] not inclusive of max.
Definition utils.c:74
double rand_normal(const double mu, const double sigma)
Returns a random Gaussian with specified mean and standard deviation.
Definition utils.c:87
double rand_uniform(const double min, const double max)
Returns a uniform random float [min,max].
Definition utils.c:62
void rand_init(void)
Initialises the pseudo-random number generator.
Definition utils.c:34
void utils_json_parse_check(const cJSON *json)
Checks whether JSON parsed correctly.
Definition utils.c:109
void rand_init_seed(const uint32_t seed)
Initialises the pseudo-random number generator with a fixed seed.
Definition utils.c:50
Utility functions for random number handling, etc.