Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Gaussian distribution of random numbers [duplicate]

Possible Duplicate:
Generating a random Gaussian double in Objective-C/C

Is there any way of getting a random number not from a uniform distribution, but from a Gaussian (Normal, Bell Curve) distribution in iOS? All the random number generators I have found are basically uniform and I want to make the numbers cluster around a certain point. Thanks!

like image 761
syzygy Avatar asked Oct 18 '12 06:10

syzygy


2 Answers

Just use a uniform distribution generator and apply the Box-Muller Transform:

double u1 = (double)arc4random() / UINT32_MAX; // uniform distribution
double u2 = (double)arc4random() / UINT32_MAX; // uniform distribution
double f1 = sqrt(-2 * log(u1));
double f2 = 2 * M_PI * u2;
double g1 = f1 * cos(f2); // gaussian distribution
double g2 = f1 * sin(f2); // gaussian distribution
like image 101
rob mayoff Avatar answered Nov 13 '22 19:11

rob mayoff


One simple option is to add several numbers from a uniform distribution together. Many dice based games use this approach to generate roughly normal distributions of results.

Distribution of rolling 3 6 sided dice

via wikipedia

If you can be more specific about what distribution you want there may be more precise solutions but combining several rolls is an easy and fairly flexible solution.

like image 30
Jonah Avatar answered Nov 13 '22 19:11

Jonah