Tscope5
randomizer_internal.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // __ ______
4 // / /_______________ ____ ___ / ____/
5 // / __/ ___/ ___/ __ \/ __ \/ _ \ /___ )
6 // / /_(__ ) /__/ /_/ / /_/ / __/ ____/ /
7 // \__/____/\___/\____/ .___/\___/ /_____/
8 // /_/
9 //
10 /// \file randomizer_internal.c
11 /// Definitions of internal randomizer functions.
12 ////////////////////////////////////////////////////////////////////////////////
13 
14 
15 #include "../include/tscope5/randomizer_internal.h"
16 #include "../include/tscope5/system_internal.h"
17 
18 #include <gsl/gsl_rng.h>
19 #include <gsl/gsl_randist.h>
20 #include <sys/time.h>
21 
22 /// Is the randomizer subsystem installed?
24 
25 
26 ////////////////////////////////////////////////////////////////////////////////
27 /// Do some checks at the start of each randomizer function.
28 ///
29 /// \param calling_function Name the function that calls
30 /// for this check or installation.
31 ///
32 /// Checks whether the randomizer subsystem is installed.
33 /// If not, the randomizer subsystem is installed.
34 ////////////////////////////////////////////////////////////////////////////////
35 void ts5_check_randomizer(char *calling_function)
36 {
37  ts5_log(TS5_LOGLEVEL_6, "%s: ts5_check_randomizer\n", calling_function);
39  ts5_install_randomizer(calling_function);
40  }
41 }
42 
43 
44 ////////////////////////////////////////////////////////////////////////////////
45 /// Install the randomizer subsystem.
46 ///
47 /// \param calling_function Name the function that calls
48 /// for this check or installation.
49 ///
50 /// The ranlux randomizer from the Gnu Scientific Library is used.
51 ///
52 /// This function is called automatically if necessary.
53 ////////////////////////////////////////////////////////////////////////////////
54 void ts5_install_randomizer(char *calling_function)
55 {
57  ts5_install_tscope5(calling_function);
58  }
59 
61 
63  ts5_log(TS5_LOGLEVEL_1, "%s: Installing Tscope5 randomizer\n",
64  calling_function);
65 
66  _ts5_data.randomizer = gsl_rng_alloc(gsl_rng_ranlxs2);
67 
68  if (!_ts5_data.randomizer) {
69  ts5_fatal("%s: could not install Tscope5 randomizer\n",
70  calling_function);
71  }
72 
73  // seed randomizer using the clock time
74  struct timeval tv;
75  gettimeofday(&tv, NULL);
76  unsigned long seed = tv.tv_usec;
77  gsl_rng_set(_ts5_data.randomizer, seed);
78  ts5_log(TS5_LOGLEVEL_1, "%s: Random seed is %lu\n",
79  calling_function, seed);
80 
82  }
83 }
84 
85 
86 ////////////////////////////////////////////////////////////////////////////////
87 /// Uninstall the randomizer subsystem.
88 ///
89 /// This function is called automatically at the end of the program.
90 ////////////////////////////////////////////////////////////////////////////////
92 {
94 
95  ts5_log(TS5_LOGLEVEL_1, "Uninstalling Tscope5 randomizer\n");
96 
97  gsl_rng_free(_ts5_data.randomizer);
98  _ts5_data.randomizer = NULL;
99 
101  }
102 }
103 
int _ts5_is_tscope5_installed
Is Tscope5 installed?
void ts5_uninstall_randomizer()
Uninstall the randomizer subsystem.
int _ts5_is_randomizer_installed
Is the randomizer subsystem installed?
void ts5_check_randomizer(char *calling_function)
Do some checks at the start of each randomizer function.
void ts5_log(const unsigned int level, const char *format,...)
Send info to a logging window.
Definition: system.c:45
void ts5_install_tscope5(char *calling_function)
Install Tscope5.
void ts5_fatal(const char *format,...)
Exit safely with an error message.
Definition: system.c:533
void ts5_install_randomizer(char *calling_function)
Install the randomizer subsystem.