Random Gaussian Double Generation in Objective-C / C

I am trying to create a random Gaussian double in Objective-C (same as random.nextGaussian in Java). However, rand_gauss() does not seem to work. Does anyone know a way to achieve this?

+7
source share
1 answer

This link shows how to calculate it using the standard random() function.

I should note that you will most likely have to execute the ranf() procedure, which converts the output of random() from [0,MAX_INT] to from [0,1] , but this should not be too complicated.

From a related article:

The polar form of Box-Muller conversion is faster and more reliable. Algorithmic description: float x1, x2, w, y1, y2;

  do { x1 = 2.0 * ranf() - 1.0; x2 = 2.0 * ranf() - 1.0; w = x1 * x1 + x2 * x2; } while ( w >= 1.0 ); w = sqrt( (-2.0 * ln( w ) ) / w ); y1 = x1 * w; y2 = x2 * w; 
+9
source

All Articles