Tscope5
randomizer.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // __ ______
4 // / /_______________ ____ ___ / ____/
5 // / __/ ___/ ___/ __ \/ __ \/ _ \ /___ )
6 // / /_(__ ) /__/ /_/ / /_/ / __/ ____/ /
7 // \__/____/\___/\____/ .___/\___/ /_____/
8 // /_/
9 //
10 /// \file randomizer.c
11 /// Definitions of randomizer functions.
12 /// \example randomizer01.c
13 /// \example randomizer02.c
14 /// \example randomizer03.c
15 ////////////////////////////////////////////////////////////////////////////////
16 
17 
18 #include "../include/tscope5/randomizer.h"
19 #include "../include/tscope5/randomizer_internal.h"
20 #include "../include/tscope5/system_internal.h"
21 
22 #include <gsl/gsl_rng.h>
23 #include <gsl/gsl_randist.h>
24 #include <sys/time.h>
25 
26 
27 ////////////////////////////////////////////////////////////////////////////////
28 /// @name Random integers
29 /// In an experiment most of the time you will need random integers,
30 /// preferably drawn from a list without replacement:
31 //@{
32 ////////////////////////////////////////////////////////////////////////////////
33 
34 
35 ////////////////////////////////////////////////////////////////////////////////
36 /// Draw an integer from a uniform distribution.
37 ///
38 /// \param max Number of possible values of the random integer.
39 ///
40 /// \return An integer from 0 to n-1.
41 ////////////////////////////////////////////////////////////////////////////////
42 unsigned long int ts5_random_integer(const unsigned long int max)
43 {
44  ts5_check_randomizer("ts5_random_integer");
45  ts5_log(TS5_LOGLEVEL_5, "ts5_random_integer(%lu)\n", max);
46 
47  return gsl_rng_uniform_int(_ts5_data.randomizer, max);
48 }
49 
50 
51 ////////////////////////////////////////////////////////////////////////////////
52 /// Draw a list of random values without replacement.
53 ///
54 /// \param nmax Maximum value (output values will range from 0 to n-1).
55 /// \param freq Frequency of each value.
56 /// \param list Adress of the array where the list is stored.
57 ////////////////////////////////////////////////////////////////////////////////
58 void ts5_random_list(const int nmax, const int freq, int *list)
59 {
60  ts5_check_randomizer("ts5_random_list");
61  ts5_log(TS5_LOGLEVEL_5, "ts5_random_list(%d,%d,%p)\n", nmax, freq, list);
62 
63  int i;
64  for (i = 0; i < nmax * freq; i++) {
65  list[i] = i % nmax;
66  }
67 
68  gsl_ran_shuffle(_ts5_data.randomizer, list, (unsigned long)(nmax * freq),
69  sizeof(int));
70 }
71 
72 ////////////////////////////////////////////////////////////////////////////////
73 //@}
74 ////////////////////////////////////////////////////////////////////////////////
75 
76 
77 ////////////////////////////////////////////////////////////////////////////////
78 /// @name Random doubles
79 /// Tscope5 also gives acces to random numbers drawn from
80 /// some common distributions:
81 //@{
82 ////////////////////////////////////////////////////////////////////////////////
83 
84 
85 ////////////////////////////////////////////////////////////////////////////////
86 /// Draw a double from a uniform distribution.
87 ///
88 /// \return A double precision floating point value from a uniform distribution.
89 ///
90 /// The range includes 0.0 but excludes 1.0.
91 ////////////////////////////////////////////////////////////////////////////////
93 {
94  ts5_check_randomizer("ts5_random_uniform");
95  ts5_log(TS5_LOGLEVEL_5, "ts5_random_uniform()\n");
96 
97  return gsl_rng_uniform(_ts5_data.randomizer);
98 }
99 
100 
101 ////////////////////////////////////////////////////////////////////////////////
102 /// Draw a double from a normal distribution.
103 ///
104 /// \param mu Mean of the distribution.
105 /// \param sigma Standard deviation of the distribution.
106 ///
107 /// \return The random value.
108 ////////////////////////////////////////////////////////////////////////////////
109 double ts5_random_normal(double mu, double sigma)
110 {
111  ts5_check_randomizer("ts5_random_normal");
112  ts5_log(TS5_LOGLEVEL_5, "ts5_random_normal(%f,%f)\n", mu, sigma);
113 
114  return (mu + gsl_ran_gaussian(_ts5_data.randomizer, sigma));
115 }
116 
117 
118 ////////////////////////////////////////////////////////////////////////////////
119 /// Draw a double from an exponential distribution.
120 ///
121 /// \param mu Mean of the distribution.
122 ///
123 /// \return The random value.
124 ////////////////////////////////////////////////////////////////////////////////
125 double ts5_random_exponential(double mu)
126 {
127  ts5_check_randomizer("ts5_random_exponential");
128  ts5_log(TS5_LOGLEVEL_5, "ts5_random_exponential(%f)\n", mu);
129 
130  return gsl_ran_exponential(_ts5_data.randomizer, mu);
131 }
132 
133 
134 ////////////////////////////////////////////////////////////////////////////////
135 /// @name Set the random seed
136 /// If you do simulation work it is also possible to set
137 /// the random seed yourself.
138 /// Identical seeds always result in the same randomized list.
139 //@{
140 ////////////////////////////////////////////////////////////////////////////////
141 
142 
143 ////////////////////////////////////////////////////////////////////////////////
144 /// Seed the randomizer subsystem.
145 ///
146 /// \param seed Seed for the randomizer.
147 ///
148 /// If the generator is seeded with the same value of seed
149 /// on two different runs, the same stream of random numbers will be generated
150 /// by successive calls to the routines.
151 ///
152 /// The timer is a good source for random seeds.
153 ////////////////////////////////////////////////////////////////////////////////
154 unsigned long int ts5_seed_randomizer(const unsigned long int seed)
155 {
156  ts5_check_randomizer("ts5_seed_randomizer");
157  ts5_log(TS5_LOGLEVEL_4, "ts5_seed_randomizer(%lu)\n", seed);
158 
159  gsl_rng_set(_ts5_data.randomizer, seed);
160 
161  return seed;
162 }
163 
164 
165 ////////////////////////////////////////////////////////////////////////////////
166 //@}
167 ////////////////////////////////////////////////////////////////////////////////
double ts5_random_exponential(double mu)
Draw a double from an exponential distribution.
Definition: randomizer.c:125
double ts5_random_normal(double mu, double sigma)
Draw a double from a normal distribution.
Definition: randomizer.c:109
void ts5_check_randomizer(char *calling_function)
Do some checks at the start of each randomizer function.
void ts5_random_list(const int nmax, const int freq, int *list)
Draw a list of random values without replacement.
Definition: randomizer.c:58
void ts5_log(const unsigned int level, const char *format,...)
Send info to a logging window.
Definition: system.c:45
unsigned long int ts5_seed_randomizer(const unsigned long int seed)
Seed the randomizer subsystem.
Definition: randomizer.c:154
unsigned long int ts5_random_integer(const unsigned long int max)
Draw an integer from a uniform distribution.
Definition: randomizer.c:42
double ts5_random_uniform()
Draw a double from a uniform distribution.
Definition: randomizer.c:92