Two random variables that sum up to user-defined value [closed]


What I am looking for is to create an application so 6 year old can learn math. This application should generate random examples, like:

1 + 6 5 + 4 3 + 1 0 + 10 9 + 1 

The sum should never exceed 10. Cases like “9 + 1” and “1 + 9” are two different cases.

I tried to generate random numbers the following way (JavaScript):

const getRandomInt = (min, max) => {   min = Math.ceil(min);   max = Math.floor(max);   return Math.floor(Math.random() * (max - min + 1)) + min; }  const hh1 = {}; const hh2 = {}; for (let i = 0; i < 10000; i++) {   const x = getRandomInt(0, 9);   const y = getRandomInt(0, 9 - x);    if (!hh1[x]) {     hh1[x] = 1;   } else {     hh1[x] = hh1[x] + 1;   }    if (!hh2[y]) {     hh2[y] = 1;   } else {     hh2[y] = hh2[y] + 1;   } } 

But it obviously didn’t work, result is:

> hh1 { '0': 1005,   '1': 1037,   '2': 952,   '3': 951,   '4': 1048,   '5': 986,   '6': 1025,   '7': 1060,   '8': 992,   '9': 944 } > hh2 { '0': 2850,   '1': 2009,   '2': 1438,   '3': 1092,   '4': 821,   '5': 643,   '6': 488,   '7': 328,   '8': 225,   '9': 106 } 

First number looks random, the the second isn’t. Zero appears 10 times more than 9, for example. One way to fix is to generate all pairs and pick from the pair. But I’m wondering if there is a better way.