mirror of
https://github.com/projectM-visualizer/projectm.git
synced 2026-03-10 17:35:24 +00:00
forgot to add random number generators to repo
git-svn-id: https://projectm.svn.sourceforge.net/svnroot/projectm/trunk@813 6778bc44-b910-0410-a7a0-be141de4315d
This commit is contained in:
19
src/projectM-engine/RandomNumberGenerators.cpp
Normal file
19
src/projectM-engine/RandomNumberGenerators.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
#fndef RANDOM_NUMBER_GENERATORS_HPP
|
||||
#define RANDOM_NUMBER_GENERATORS_HPP
|
||||
#include <cmath>
|
||||
|
||||
namespace RandomNumberGenerators {
|
||||
|
||||
double gaussian( double min = 0, double max=1, double sigma=1, double mean=.5)
|
||||
{
|
||||
double random = (min + (max-min) * (double)rand()/RAND_MAX); //create random domain between [min,max]
|
||||
double tmp = (random-mean)/sigma;
|
||||
double gauss= exp(-tmp*tmp/2); //gaussian formula
|
||||
|
||||
return gauss;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
55
src/projectM-engine/RandomNumberGenerators.hpp
Normal file
55
src/projectM-engine/RandomNumberGenerators.hpp
Normal file
@ -0,0 +1,55 @@
|
||||
#ifndef RANDOM_NUMBER_GENERATORS_HPP
|
||||
#define RANDOM_NUMBER_GENERATORS_HPP
|
||||
#include <cmath>
|
||||
|
||||
namespace RandomNumberGenerators {
|
||||
|
||||
inline float uniform()
|
||||
/* 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. */
|
||||
{
|
||||
const int ia=16807,ic=2147483647,iq=127773,ir=2836;
|
||||
int il,ih,it;
|
||||
float 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 float gaussian(float mean, float sigma)
|
||||
{
|
||||
|
||||
float x1, x2, w, y1, y2;
|
||||
|
||||
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;
|
||||
y2 = x2 * w;
|
||||
|
||||
float ret = y1*sigma + mean;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user