2

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));
}
Vulkan
  • 1,004
  • 16
  • 44
  • Does this answer your question? [Converting a Uniform Distribution to a Normal Distribution](https://stackoverflow.com/questions/75677/converting-a-uniform-distribution-to-a-normal-distribution) – Peter O. Apr 28 '21 at 15:01
  • Are you sure you want to base your results on a normal? 1) The support for a normal is infinite, so you'd actually have to generate a truncated normal. 2) If you use a small standard deviation then you'll almost never get low values or high ones; if you use a large standard deviation you'll likely have to throw away a lot of values that are out of range. Would you consider other distributions which have bounded integer support, such as [binomials](https://en.wikipedia.org/wiki/Binomial_distribution) or discrete [triangles](https://en.wikipedia.org/wiki/Triangular_distribution)? – pjs Apr 28 '21 at 20:41

1 Answers1

1

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];