Where can I find or how can I make a number generator from a number? For example, I have the number 66, I need to scatter it into X parts, so that adding all these parts will result in the same number.

  • one
    And what conditions are imposed on the numbers? If none, take X - 1 units of units, and one piece 66 - Х + 1 . Or generally X - 1 pieces of zeros, and one piece 66 . - VladD

2 answers 2

In short, the code is easier to write than in words :)

 var out = [], X= 8, Y = 111111; for (var i = X-1; i > 0; i--) { var p = Math.floor(Math.random() * (Y - i))+1; Y -= p; out.push(p); } out.push(Y); console.log(out); 

http://jsfiddle.net/oceog/rgHL6/

This is if there are no other restrictions.

like that, but it moves to the beginning

  • but where are the doubts? - DreamChild
  • one
    jvas! = javascript you probably still don’t understand much. - zb '

Here is another idea for a more even distribution (sorry, just an idea):

  1. Let X be the number of numbers, Y desired amount.
  2. We generate X random numbers from 0 to 1.
  3. We summarize them, let S be their sum.
  4. Put K = Y / S , multiply all random numbers by K
  5. If you need integers , round off, and if you need to adjust the amount, modify the last term.