javascript - Generate an array that adds up to specific value using only 2 numbers? -


i need randomly generate "grid" total of integers in array add 8 (or #) using 1 , 2.

example output be:

[1, 2, 2, 1, 2] 

or

[1, 1, 2, 1, 1, 1, 1] 

or

[2, 2, 2, 2] 

this how generate random numbers in array add defined total? js.

this should it:

for (var res=[], sum=0; sum<8; sum+=res[res.length-1])     res.push(math.floor(1 + math.random() * math.min(8-sum, 2))); 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -