• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nasa / trick / 25456501308

06 May 2026 07:29PM UTC coverage: 55.935% (-0.8%) from 56.7%
25456501308

Pull #2011

github

web-flow
Merge 7ad262960 into 7054e405e
Pull Request #2011: Single-file CI and code style adoption

14612 of 26123 relevant lines covered (55.94%)

462107.16 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

59.23
/trick_source/sim_services/MonteCarlo/MonteVarRandom.cpp
1
#include <iostream>
2
#include <sstream>
3
#include <stdio.h>
4
#include <cmath>
5
#include <limits>
6

7
#include "trick/MonteVar.hh"
8
#include "trick/MonteVarRandom.hh"
9
#include "trick/exec_proto.h"
10

11
Trick::MonteVarRandom::MonteVarRandom(std::string in_name, Distribution in_distribution, std::string in_unit, StlEngine in_engine) : engineType(in_engine), randist(), stlGenPtr(0) {
8✔
12
    this->name = in_name;
8✔
13
    this->distribution = in_distribution;
8✔
14
    this->unit = in_unit;
8✔
15

16
    // perform this init in all cases, in case fall back to Trick-coded distributions
17
    trick_gsl_rand_init(&randist);
8✔
18

19
    if (NO_ENGINE != engineType) {
8✔
20
        // minimum set of randist defaults when using C++11 distributions
21
        randist.mu = 0.0;
1✔
22
        randist.sigma = 1.0;
1✔
23
        randist.max = std::numeric_limits<double>::max();
1✔
24
        randist.min = - randist.max;
1✔
25
        randist.rel_min = 1;
1✔
26
        randist.rel_max = 1;
1✔
27
        randist.sigma_range = 0.0;
1✔
28
    }
29
    randist.type = (TRICK_GSL_TYPE) distribution;
8✔
30
    randist.sigma_range = 1;
8✔
31

32
#if (defined(_HAVE_TR1_RANDOM) || defined(_HAVE_STL_RANDOM))
33
    unsigned long seed = randist.seed;
34
    double unused = 0.0;
35
    StlRandomGenerator::StlDistribution stlDist =
36
        static_cast<StlRandomGenerator::StlDistribution>(distribution);
37
    StlRandomGenerator::StlEngine stlEngine =
38
        static_cast<StlRandomGenerator::StlEngine>(engineType);
39
    if (engineType != NO_ENGINE) {
40
        // note: in practice, these will have to be changed by calling set_mu, etc,
41
        // in the input file after construction.
42

43
        switch (randist.type) {
44
        case GAUSSIAN:
45
            stlGenPtr = StlRandomGeneratorFactory::newGenerator(randist.mu, randist.sigma,
46
                        seed, stlDist, stlEngine);
47
            break;
48
        case FLAT:
49
            // StlRandomGenerator min,max are always absolute
50
            stlGenPtr = StlRandomGeneratorFactory::newGenerator(
51
                        get_absolute_min(), get_absolute_max(),
52
                        seed, stlDist, stlEngine);
53
            break;
54
        case POISSON:
55
            stlGenPtr = StlRandomGeneratorFactory::newGenerator(randist.mu, unused,
56
                        seed, stlDist, stlEngine);
57
            break;
58
        default:
59
            break;
60
        }
61
    }
62
#endif
63
}
8✔
64

65
Trick::MonteVarRandom::~MonteVarRandom()
8✔
66
{
67
    delete stlGenPtr;
8✔
68
}
8✔
69

70
// Composite the various properties of this MonteVarRandom.
71
std::string Trick::MonteVarRandom::describe_variable()
×
72
{
73
    std::string dist_list[] = {"GAUSSIAN", "FLAT", "POISSON"};
×
74
    std::stringstream ss;
×
75

76
    ss << "#NAME:\t\t\t" << this->name << "\n"
×
77
       << "#TYPE:\t\t\tRANDOM\n" 
78
       << "#UNIT:\t\t\t" << this->unit << "\n"
×
79
       << "#DISTRIBUTION:\t" << dist_list[this->randist.type] << "\n"
×
80
       << "#SEED:\t\t\t" << this->randist.seed << "\n"
×
81
       << "#SIGMA:\t\t\t" << this->randist.sigma << "\n"
×
82
       << "#MU:\t\t\t" << this->randist.mu << "\n"
×
83
       << "#MIN:\t\t\t" << this->randist.min << "\n"
×
84
       << "#MAX:\t\t\t" << this->randist.max << "\n"
×
85
       << "#REL_MIN:\t\t" << this->randist.rel_min << "\n"
×
86
       << "#REL_MAX:\t\t" << this->randist.rel_max << "\n";
×
87

88
    return ss.str();
×
89
}
×
90

91
void Trick::MonteVarRandom::set_seed(unsigned long seed) {
7✔
92
    randist.seed = seed;
7✔
93
    if (engineType != NO_ENGINE && stlGenPtr) {
7✔
94
        stlGenPtr->set_seed(seed);
×
95
    } else {
96
        trick_gsl_rand_seed(&randist);
7✔
97
    }
98
}
7✔
99

100
void Trick::MonteVarRandom::set_sigma(double sigma) {
3✔
101
    if (engineType != NO_ENGINE && TRICK_GSL_GAUSS == randist.type && stlGenPtr) {
3✔
102
        randist.sigma = sigma;
×
103
        updateStlRandom();
×
104
    } else {
105
        randist.sigma = sigma;
3✔
106
    }
107
}
3✔
108

109
void Trick::MonteVarRandom::set_sigma_range(int sigma_range) {
5✔
110
    randist.sigma_range = sigma_range;
5✔
111
}
5✔
112

113
void Trick::MonteVarRandom::set_mu(double mu) {
8✔
114
    if (engineType != NO_ENGINE && stlGenPtr) {
8✔
115
        randist.mu = mu;
×
116
        updateStlRandom();
×
117
    } else {
118
        randist.mu = mu;
8✔
119
    }
120
}
8✔
121

122
void Trick::MonteVarRandom::set_min(double min) {
8✔
123
    if (engineType != NO_ENGINE && stlGenPtr) {
8✔
124
        randist.min = min;
×
125
        updateStlRandom();
×
126
    } else {
127
        randist.min = min;
8✔
128
    }
129
}
8✔
130

131
void Trick::MonteVarRandom::set_max(double max) {
8✔
132
    if (engineType != NO_ENGINE && stlGenPtr) {
8✔
133
        randist.max = max;
×
134
        updateStlRandom();
×
135
    } else {
136
        randist.max = max;
8✔
137
    }
138
}
8✔
139

140
void Trick::MonteVarRandom::set_min_is_relative(bool relative) {
1✔
141
    randist.rel_min = (int)relative;
1✔
142
    updateStlRandom();
1✔
143
}
1✔
144

145
void Trick::MonteVarRandom::set_max_is_relative(bool relative) {
1✔
146
    randist.rel_max = (int)relative;
1✔
147
    updateStlRandom();
1✔
148
}
1✔
149

150
void Trick::MonteVarRandom::set_uniform_generator(uniform_generator uniform) {
2✔
151
    randist.uniform = uniform;
2✔
152
}
2✔
153

154
std::string Trick::MonteVarRandom::get_next_value() {
1,207✔
155
    TRICK_GSL_RETURN_TYPE return_value;
156
    char buffer[128];
157

158
    return_value.d = 0;
1,207✔
159
    if (stlGenPtr) {
1,207✔
160
        double sigma_range = static_cast<double>(randist.sigma_range) * randist.sigma;
×
161
        double min = get_absolute_min();
×
162
        double max = get_absolute_max();
×
163

164
        unsigned int count = 0;
×
165
        while (count < 100) {
×
166
            return_value = (*stlGenPtr)();
×
167
            count++;
×
168

169
            if (return_value.d < min || return_value.d > max) {
×
170
                continue;
×
171
            } else {
172

173
                if (0 == randist.sigma_range // 0== means sigma_range algorithm is turned off
×
174
                    || std::fabs(return_value.d - randist.mu) <= sigma_range) {
×
175
                    break;
176
                }
177
            }
178
        }
179
        if (count >= 100) {
×
180
            char string[100];
181
            snprintf(string, sizeof(string), "Trick:MonteVarRandom failed to generate a random value for variable \"%s\"\n", name.c_str());
×
182
            exec_terminate_with_return(-1, __FILE__, __LINE__, string);
×
183
        }
184

185
    } else {
186
        if (trick_gsl_rand(&randist, &return_value) != 0) {
1,207✔
187
            char string[100];
188
            snprintf(string, sizeof(string), "Trick:MonteVarRandom failed to generate a random value for variable \"%s\"\n", name.c_str());
1✔
189
            exec_terminate_with_return(-1, __FILE__, __LINE__, string);
1✔
190
        }
191
    }
192

193
    switch (randist.type) {
1,206✔
194
        case TRICK_GSL_POISSON:
1✔
195
            // STL returns int, GSL returns unsigned int
196
            if (stlGenPtr) {
1✔
197
                snprintf(buffer, sizeof(buffer), " %d", return_value.ii);
×
198
            } else {
199
                snprintf(buffer, sizeof(buffer), " %u", return_value.ui);
1✔
200
            }
201
            value = buffer;
1✔
202
            break;
1✔
203
        case TRICK_GSL_GAUSS:
1,205✔
204
        case TRICK_GSL_FLAT:
205
        default:
206
            snprintf(buffer, sizeof(buffer), "%.15g", return_value.d);
1,205✔
207
            value = buffer;
1,205✔
208
            break;
1,205✔
209
    }
210

211
    if (unit.empty()) {
1,206✔
212
        return name + std::string(" = ") + value ;
4,820✔
213
    } else {
214
        return name + std::string(" = trick.attach_units(\"") + unit + std::string("\", ")  + value +
5✔
215
               std::string(")") ;
4✔
216
    }
217
}
218

219
///@details Update the full set of STL random settings
220
/// (Call after any of the settings: randist.mu .set_min, max, min_is_relative, max_is_relative
221
/// are called.)
222
void
223
Trick::MonteVarRandom::updateStlRandom() {
2✔
224

225
    if (stlGenPtr) {
2✔
226

227
        switch (randist.type) {
×
228
        case GAUSSIAN:
×
229
            stlGenPtr->set_param(randist.mu, randist.sigma);
×
230
            break;
×
231
        case FLAT:
×
232
            // StlRandomGenerator min,max are always absolute
233
            stlGenPtr->set_param(get_absolute_min(), get_absolute_max());
×
234
            break;
×
235
        case POISSON:
×
236
            stlGenPtr->set_param(randist.mu);
×
237
            break;
×
238
        default:
×
239
            break;
×
240
        }
241
    }
242
}
2✔
243

244
const TRICK_GSL_RANDIST& Trick::MonteVarRandom::get_random_distribution() {
×
245
    return randist;
×
246
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc