forked from karpathy/scriptsbots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.h
39 lines (36 loc) · 1.06 KB
/
helpers.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#ifndef HELPERS_H
#define HELPERS_H
#include <stdlib.h>
#include <math.h>
//uniform random in [a,b)
inline float randf(float a, float b){return ((b-a)*((float)rand()/RAND_MAX))+a;}
//uniform random int in [a,b)
inline int randi(int a, int b){return (rand()%(b-a))+a;}
//normalvariate random N(mu, sigma)
inline double randn(double mu, double sigma) {
static bool deviateAvailable=false; // flag
static float storedDeviate; // deviate from previous calculation
double polar, rsquared, var1, var2;
if (!deviateAvailable) {
do {
var1=2.0*( double(rand())/double(RAND_MAX) ) - 1.0;
var2=2.0*( double(rand())/double(RAND_MAX) ) - 1.0;
rsquared=var1*var1+var2*var2;
} while ( rsquared>=1.0 || rsquared == 0.0);
polar=sqrt(-2.0*log(rsquared)/rsquared);
storedDeviate=var1*polar;
deviateAvailable=true;
return var2*polar*sigma + mu;
}
else {
deviateAvailable=false;
return storedDeviate*sigma + mu;
}
}
//cap value between 0 and 1
inline float cap(float a){
if (a<0) return 0;
if (a>1) return 1;
return a;
}
#endif