diff --git a/src/libprojectM/CMakeLists.txt b/src/libprojectM/CMakeLists.txt index 14ff4b52b..63ec426d3 100644 --- a/src/libprojectM/CMakeLists.txt +++ b/src/libprojectM/CMakeLists.txt @@ -21,7 +21,6 @@ add_library(projectM_main OBJECT ProjectM.hpp ProjectMCWrapper.cpp ProjectMCWrapper.hpp - RandomNumberGenerators.hpp TimeKeeper.cpp TimeKeeper.hpp projectM-opengl.h diff --git a/src/libprojectM/RandomNumberGenerators.hpp b/src/libprojectM/RandomNumberGenerators.hpp deleted file mode 100644 index 43b179491..000000000 --- a/src/libprojectM/RandomNumberGenerators.hpp +++ /dev/null @@ -1,116 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -#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 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(weights.size()-1); -} - -inline uint32_t weightedRandom(const std::vector & 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(weights.size()-1); -} - -} // namespace RandomNumberGenerators -} // namespace libprojectM diff --git a/src/libprojectM/TimeKeeper.cpp b/src/libprojectM/TimeKeeper.cpp index 25fee921f..2989afbd2 100644 --- a/src/libprojectM/TimeKeeper.cpp +++ b/src/libprojectM/TimeKeeper.cpp @@ -1,7 +1,5 @@ #include "TimeKeeper.hpp" -#include "RandomNumberGenerators.hpp" - #include namespace libprojectM { @@ -107,8 +105,8 @@ double TimeKeeper::PresetTimeA() double TimeKeeper::sampledPresetDuration() { - return std::max(1, RandomNumberGenerators::gaussian(m_presetDuration, m_easterEgg)); - + std::normal_distribution gaussianDistribution{m_presetDuration, m_easterEgg}; + return std::max(1.0, gaussianDistribution(m_randomGenerator)); } } // namespace libprojectM diff --git a/src/libprojectM/TimeKeeper.hpp b/src/libprojectM/TimeKeeper.hpp index 42a5949b6..23d17df6c 100644 --- a/src/libprojectM/TimeKeeper.hpp +++ b/src/libprojectM/TimeKeeper.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include 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{};