declare 40 < A < 60,20 < B < 80 and A + B = = 100
how to randomly generate An and B?
first modification:
statement 40 < A < 60,20 < B < 80,30 < C < 70 and A + B + C = = 150
how to randomly generate A, B and C?
Update
uses Cucumber"s algorithm and writes
function main () {
let sum = 24;
let index = 0;
// A[1,5], B[0,6], C[2,4], D[2,4], E[0,6], F[1,5], G[2,4], H[0,6]
let maxs = [ 5, 6, 4, 4, 6, 5, 4, 6,]; //
let mins = [ 1, 0, 2, 2, 0, 1, 2, 0,]; //
let result = []; //
while (index < maxs.length - 1) {
// [] [] // [1,5] (24-0-2-2-0-1-2-0 === 17)
let max = Math.min(maxs[index], sum - mins.slice(index + 1).reduce((o, n) => o + n));
//
let min = Math.max(mins[index], sum - maxs.slice(index + 1).reduce((o, n) => o + n));
//
result[index] = Math.floor(Math.random() * ((max - min) + 1)) + min;
//
sum -= result[index];
indexPP;
}
//
result[maxs.length - 1] = sum;
return result ;
}
but there is something wrong with this result, that is, the distribution is very uneven
after the execution of the main method above
var x = [0,0,0,0,0,0,0,0].map(v=>new Array(7).join(".").split(".").map(u => 0));
var N = 100000;
for(var i = 0;i<N;iPP){
var r = main();
for(var j = 0; j<8;jPP){
x[j][r[j]] PP
}
}
console.log(x)
the number of occurrences of each number in the results of each group is
[[ 0, 20041, 19843, 20108, 20210, 19798, 0], // A
[14193, 14407, 14449, 14394, 14280, 14137, 14140], // B
[ 0, 0, 33219, 33301, 33480, 0, 0], // C
[ 0, 0, 33080, 33519, 33401, 0, 0], // D
[13194, 14225, 15147, 15090, 14927, 14273, 13144], // E
[ 0, 20608, 19647, 19446, 19445, 20854, 0], // F
[ 0, 0, 35357, 29025, 35618, 0, 0], // G
[20940, 12813, 10764, 10847, 10907, 12756, 20973]] // H
it is obvious that the last number, H [0 br 6], has 20940 occurrences of 0 much more than 3 times 10847
and this is much less common in A than in H < 0 >. Why? How to avoid it?