0

I want to generate 4 random numbers between 1 and 100 so that the total adds up to 100. How do I do that?

say result1) 20,10,40,30 result2 ) 33,33,33,1 etc. Thanks

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • See this answer. It's not Swift, but it explains the algorithm. http://stackoverflow.com/a/18600737/1630618 – vacawama Jul 05 '15 at 10:22

1 Answers1

1

The way to go is:

Step 1: Generate four random numbers between 0 and 1
Step 2: Add these four numbers
Step 3: Divide each of the four numbers by the sum,
Step 4: Multiply by 100, and round to the nearest integer.
lakshmen
  • 28,346
  • 66
  • 178
  • 276
  • 1
    This is roughly the method used in http://stackoverflow.com/a/30156889/1187415 which is not unbiased. – Martin R Jul 05 '15 at 10:53