Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The correct answer here is unbelievably simple.</p> <p>Just imagine a white line, let's say 1000 units long.</p> <p>You want to divide the line in to ten parts, using red marks.</p> <p>VERY SIMPLY, CHOOSE NINE RANDOM NUMBERS and put a red paint mark at each of those points.</p> <p>It's just that simple. You're done!</p> <p>Thus, the algorithm is:</p> <p>(1) pick nine random numbers between 0 and 1000 (2) put the nine numbers, a zero, and a 1000, in an array (3) sort the array (4) using subtraction get the ten "distances" between array values</p> <p>You're done.</p> <p>(Obviously if you want to have no zeros in your final set, in part (1) simply rechoose another random number if you get a collision.)</p> <p>Ideally as programmers, we can "see" visual algorithms like this in our heads -- try to think visually whatever we do!</p> <hr> <p>Footnote - for any non-programmers reading this, just to be clear pls note that this is like "the first thing you ever learn when studying computer science!" i.e. I do not get any credit for this, I just typed in the answer since I stumbled on the page. No kudos to me!</p> <p>Just for the record another common approach (depending on the desired outcome, whether you're dealing with real or whole numbers, and other constraints) is also very "ah hah!" elegant. All you do is this: get 10 random numbers. Add them up. Remarkably simply, just: <em>multiply or divide them all by some number, so that, the total is the desired total!</em> It's that easy!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload