How to produce Gaussian random (nextGaussian) in IPhone?


I am a new in IPHONE developing. Now I have a project which was written by processing before and I want to translate it into OFW_IPHONE. It has nextGaussian() function inside produced by java. I can not find any gaussian random function in both OFW and OFW+IPHONE.

I try to modify the math.cpp in the openframeworks folder. Here is the code:

double nextGaussian() {
if (haveNextNextGaussian ) {
haveNextNextGaussian = false;
return nextNextGaussian;
} else {
double v1, v2, s;
do {
v1 = 2 * (double)ofRandomf() - 1; // between -1.0 and 1.0
v2 = 2 * (double)ofRandomf() - 1; // between -1.0 and 1.0
s = v1 * v1 + v2 * v2;
} while (s >= 1 || s == 0);
double multiplier = sqrt(-2 * log(s)/s);
nextNextGaussian = v2 * multiplier;
haveNextNextGaussian = true;
return v1 * multiplier;

and I add

bool haveNextNextGaussian ;
double nextNextGaussian;

in the math.h;

but it can not built correctly. Both in OFW and OFW+IPHONE, they pop up the error message.

Is there anyone can give me some help to finish the Gaussian random in the OFW Iphone??

Thanks a lot.

Hi, what error message are you getting?

Problem solved.

I forgot to put “extern” in front of the variables.

better if you declare that vars in the .cpp since you don’t need to use them from outside the function. Also try to not modify the OF core, when a new release comes out, your code will not work anymore, keeping your functions in your own code makes it compatible with future versions.

That can be a useful addition to OF though, i’ve posted it as an issue here:…-/issue/224


I have made an addon for this, available here: