Remove RandomNumberGenerators.hpp and replace the only invocation with std::normal_distribution

This commit is contained in:
Kai Blaschke
2024-02-01 16:12:54 +01:00
parent 0789c54404
commit df48f1bb07
4 changed files with 6 additions and 121 deletions

View File

@ -21,7 +21,6 @@ add_library(projectM_main OBJECT
ProjectM.hpp
ProjectMCWrapper.cpp
ProjectMCWrapper.hpp
RandomNumberGenerators.hpp
TimeKeeper.cpp
TimeKeeper.hpp
projectM-opengl.h

View File

@ -1,116 +0,0 @@
#pragma once
#include <cmath>
#include <vector>
#include <cassert>
#include <iostream>
#define WEIGHTED_RANDOM_DEBUG 0
namespace libprojectM {
namespace RandomNumberGenerators {
/* Uniform random number generator x(n+1)= a*x(n) mod c
with a = pow(7,5) and c = pow(2,31)-1.
Copyright (c) Tao Pang 1997. */
inline double uniform()
{
const int ia=16807,ic=2147483647,iq=127773,ir=2836;
int il,ih,it;
double rc;
static int iseed = rand();
ih = iseed/iq;
il = iseed%iq;
it = ia*il-ir*ih;
if (it > 0)
{
iseed = it;
}
else
{
iseed = ic+it;
}
rc = ic;
return iseed/rc;
}
inline double gaussian(double mean, double sigma)
{
double x1, x2, w, y1;
do {
x1 = 2.0 * uniform() - 1.0;
x2 = 2.0 * uniform() - 1.0;
w = x1 * x1 + x2 * x2;
} while ( w >= 1.0 );
w = sqrt( (-2.0 * log( w ) ) / w );
y1 = x1 * w;
double ret = y1*sigma + mean;
return ret;
}
inline uint32_t uniformInteger(uint32_t upperBound=1)
{
/// @bug there was a man entry about how this leads to a lousy uniform
/// @bug distribution in practice. should probably review
assert(upperBound > 0);
return (rand() % upperBound);
}
/// Randomizes from probabilistically weighted distribution. Thus,
/// sum of passed in weights should be 1.0
inline uint32_t weightedRandomNormalized(std::vector<float> weights)
{
// Choose a random bounded mass between 0 and 1
float cutoff = (float)rand() / (float)RAND_MAX;
//std::cout << "cutoff : " << cutoff << std::endl;
// Sum up mass, stopping when cutoff is reached. This is the typical
// weighted sampling algorithm.
float mass = 0;
for (uint32_t i = 0; i < weights.size() ; i++) {
mass += weights[i];
//std::cout << "mass: " << mass << std::endl;
if (mass >= cutoff)
return i;
}
// Just in case something slips through the cracks
return static_cast<uint32_t>(weights.size()-1);
}
inline uint32_t weightedRandom(const std::vector<int> & weights, unsigned int weightTotalHint = 0)
{
if (weightTotalHint == 0) {
for (uint32_t i = 0; i < weights.size();i++)
weightTotalHint += weights[i];
}
const uint32_t sampledSum = uniformInteger(weightTotalHint);
uint32_t sum = 0;
if (WEIGHTED_RANDOM_DEBUG) std::cout << "[RNG::weightedRandom()] weightTotal = " << weightTotalHint <<
std::endl;
for (uint32_t i = 0; i < weights.size();i++) {
if (WEIGHTED_RANDOM_DEBUG)
std::cout << "[RNG::weightedRandom()] weight[" << i << "] = " << weights[i] <<
std::endl;
sum += weights[i];
if (sampledSum <= sum) {
if (WEIGHTED_RANDOM_DEBUG)
std::cout << "[RNG::weightedRandom()] sampled index " << i << "(" <<
"running sum = " << sum << ", sampled sum = " << sampledSum << std::endl;
return i;
}
}
return static_cast<uint32_t>(weights.size()-1);
}
} // namespace RandomNumberGenerators
} // namespace libprojectM

View File

@ -1,7 +1,5 @@
#include "TimeKeeper.hpp"
#include "RandomNumberGenerators.hpp"
#include <algorithm>
namespace libprojectM {
@ -107,8 +105,8 @@ double TimeKeeper::PresetTimeA()
double TimeKeeper::sampledPresetDuration()
{
return std::max<double>(1, RandomNumberGenerators::gaussian(m_presetDuration, m_easterEgg));
std::normal_distribution<double> gaussianDistribution{m_presetDuration, m_easterEgg};
return std::max<double>(1.0, gaussianDistribution(m_randomGenerator));
}
} // namespace libprojectM

View File

@ -1,6 +1,7 @@
#pragma once
#include <chrono>
#include <random>
namespace libprojectM {
@ -89,6 +90,9 @@ private:
/* The first ticks value of the application */
std::chrono::high_resolution_clock::time_point m_startTime{std::chrono::high_resolution_clock::now()};
std::random_device m_randomDevice{};
std::mt19937 m_randomGenerator{m_randomDevice()};
double m_secondsSinceLastFrame{};
double m_easterEgg{};