summaryrefslogtreecommitdiff
path: root/src/velocity.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/velocity.cc')
-rw-r--r--src/velocity.cc51
1 files changed, 29 insertions, 22 deletions
diff --git a/src/velocity.cc b/src/velocity.cc
index 04d0475..c8faa32 100644
--- a/src/velocity.cc
+++ b/src/velocity.cc
@@ -29,33 +29,40 @@
#include <stdlib.h>
Velocity::Velocity(unsigned int lower, unsigned int upper)
+ : lower{lower}
+ , upper{upper}
+ , samples{}
{
- this->lower = lower;
- this->upper = upper;
}
-void Velocity::addSample(Sample *sample, float probability)
+void Velocity::addSample(Sample* sample, float probability)
{
- if(samples.find(sample) != samples.end()) {
- samples[sample] += probability;
- } else {
- samples[sample] = probability;
- }
+ if(samples.find(sample) != samples.end())
+ {
+ samples[sample] += probability;
+ }
+ else
+ {
+ samples[sample] = probability;
+ }
}
-Sample *Velocity::getSample()
+Sample* Velocity::getSample()
{
- Sample *sample = NULL;
-
- float x = (float)rand() / (float)RAND_MAX;
- float sum = 0.0;
-
- Samples::iterator i = samples.begin();
- while(i != samples.end() && x > sum) {
- sum += i->second;
- sample = i->first;
- i++;
- }
-
- return sample;
+ Sample* sample{nullptr};
+
+ float x = rand.floatInRange(0, 1);
+ float sum = 0.0;
+
+ for (auto const & pair: samples)
+ {
+ if (x > sum)
+ {
+ break;
+ }
+ sum += pair.second;
+ sample = pair.first;
+ }
+
+ return sample;
}