How can I generate random numbers which fall in a normal distribution pattern?

Viewed 43

I will need to initialize an 'n' number of classes that will have a random value attached to them from 1 to 100.

I know how to create a random value between 1 and 100 but how can I make all the values to fall in a the normal distribution pattern?

+ (int)randomIntBetweenMinimum:(int)minimum maximum:(int)maximum {
    
    return (int)(minimum + arc4random_uniform(maximum - minimum + 1.0f));
}
1 Answers

There are some APIs to generate a Gaussian Distribution.

I believe that you can do the following to get what you're looking for:

GKRandomSource *random = [[GKRandomSource alloc] init];
GKRandomDistribution *distribution;
distribution = [[GKGaussianDistribution alloc]
              initWithRandomSource:random lowestValue:1 highestValue:100];

int randomVal = [distribution nextInt];
Related