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:
w1z7ard
2008-02-10 20:24:01 +00:00
parent a1bbf6f3f3
commit fc728cb22b
2 changed files with 74 additions and 0 deletions

View 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

View 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