But that's less efficient since 100 is more than 3 times larger than 30. Even with a shuffle, you'd only call the random function 60 times with the original idea + shuffle, not 100 like with this one. (and you should be using a good random function, therefore likely the bottleneck)
(I'm talking about algorithm complexity here, not micro optimizations, so it applies just fine to JS )
Your first method loops 30 times and calls the random function 30 times, but it generates poor random distribution. To make it better, you can shuffle after it (which requires another 30 iterations and random calls). So your first method + a shuffle would have 30+30 = 60 calls to the random function.
The second method loops 100 times and calls the random function 100 times.
Needless to say, 60 is smaller than 100 by a quite large margin (almost half of it). So your first method + a shuffle is probably faster than quirck's method because it calls the random function 60 times instead of 100 times.
If I'm not mistaken, this code doesn't call random only 30 times.
The idea is: repeatedly looping through the array, on each iteration randomly decide whether the current element should be incremented or not. Repeat until we have sum of elements equal to 100. So 100 is the minimum number of calls to random when always r > 0.5.
If you are worried about number of calls to random, compare it to call to random(900) and dividing it by 30 to get random quotient and random remainder.
i probably could use random 0 to 10 (or day max value), assign the value to day 1, 100 minus day 1 and repeat random,
Of course, when doing the randomness in this way, you need a non-uniform distribution (gaussian) centered around 1 or so (so day 1 being 100 being extremely unlikely, which would render the others zero).
You can use an approximate function instead of gaussian since it's easier to compute etc but you might have to use floats and stuff then (random between 0-1 float, then map it to 0-100 with a curve that approximates gaussian, just a simple function nothing fancy).
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum