blkerai blkerai - 4 months ago 7
iOS Question

How to generate 4 random number and those 4 number sum must be 100

i want get 4 different random number and their total must be 100 (rN1+rN2+rN3+rn4=100)

Answer

Just like this:

NSInteger r1, r2, r3, r4;
r1 = arc4random_uniform(96);
r2 = arc4random_uniform(97-r1);
r3 = arc4random_uniform(98-r1-r2);
r4 = 100-r1-r2-r3;