如何在匹配javascript中的固定和的范围内生成n个随机数?
How to generate n random numbers within a range matching a fixed sum in javascript?
我是 javascript 的新手,不知道如何解决这个问题。我有一个固定值 88.3,我需要 6.0 到 9.99 之间的 12 个随机数,当我将它们全部相加时,它们匹配 88.3.
到目前为止,我使用以下代码成功地生成了一个范围内的随机数:
/**
* Returns a random number between min (inclusive) and max (exclusive)
*/
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
/**
* Returns a random integer between min (inclusive) and max (inclusive).
* The value is no lower than min (or the next integer greater than min
* if min isn't an integer) and no greater than max (or the next integer
* lower than max if max isn't an integer).
* Using Math.round() will give you a non-uniform distribution!
*/
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
有人能帮帮我吗?
这是实现预期结果的一种方法。
最初的解决方案是这样的: 我已修改以说明数字范围限制。
请注意,可能有更好的方法来执行此操作,但目前可以这样做:
function generate({
targetSum,
numberCount,
minNum,
maxNum
}) {
var r = []
var currsum = 0
for (var i = 0; i < numberCount; i++) {
r[i] = Math.random() * (maxNum - minNum) + minNum
currsum += r[i]
}
let clamped = 0
let currentIndex = 0
while (currsum !== targetSum && clamped < numberCount) {
let currNum = r[currentIndex]
if (currNum == minNum || currNum == maxNum) {
currentIndex++
if (currentIndex > numberCount - 1) currentIndex = 0
continue
}
currNum += (targetSum - currsum) / 3
if (currNum <= minNum) {
r[currentIndex] = minNum + Math.random()
clamped++
} else if (currNum >= maxNum) {
r[currentIndex] = maxNum - Math.random()
clamped++
} else {
r[currentIndex] = currNum
}
currsum = r.reduce((p, c) => p + c)
currentIndex++
if (currentIndex > numberCount - 1) currentIndex = 0
}
if (currsum !== targetSum) {
console.log(`\nTargetSum: ${targetSum} can't be reached with the given options`)
}
return r
}
const numbers = generate({
targetSum: 88.3,
numberCount: 12,
minNum: 6,
maxNum: 9.99,
})
console.log('number = ', numbers)
console.log(
'Sum = ',
numbers.reduce((p, c) => p + c)
)
我是 javascript 的新手,不知道如何解决这个问题。我有一个固定值 88.3,我需要 6.0 到 9.99 之间的 12 个随机数,当我将它们全部相加时,它们匹配 88.3.
到目前为止,我使用以下代码成功地生成了一个范围内的随机数:
/**
* Returns a random number between min (inclusive) and max (exclusive)
*/
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
/**
* Returns a random integer between min (inclusive) and max (inclusive).
* The value is no lower than min (or the next integer greater than min
* if min isn't an integer) and no greater than max (or the next integer
* lower than max if max isn't an integer).
* Using Math.round() will give you a non-uniform distribution!
*/
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
有人能帮帮我吗?
这是实现预期结果的一种方法。
最初的解决方案是这样的: 我已修改以说明数字范围限制。
请注意,可能有更好的方法来执行此操作,但目前可以这样做:
function generate({
targetSum,
numberCount,
minNum,
maxNum
}) {
var r = []
var currsum = 0
for (var i = 0; i < numberCount; i++) {
r[i] = Math.random() * (maxNum - minNum) + minNum
currsum += r[i]
}
let clamped = 0
let currentIndex = 0
while (currsum !== targetSum && clamped < numberCount) {
let currNum = r[currentIndex]
if (currNum == minNum || currNum == maxNum) {
currentIndex++
if (currentIndex > numberCount - 1) currentIndex = 0
continue
}
currNum += (targetSum - currsum) / 3
if (currNum <= minNum) {
r[currentIndex] = minNum + Math.random()
clamped++
} else if (currNum >= maxNum) {
r[currentIndex] = maxNum - Math.random()
clamped++
} else {
r[currentIndex] = currNum
}
currsum = r.reduce((p, c) => p + c)
currentIndex++
if (currentIndex > numberCount - 1) currentIndex = 0
}
if (currsum !== targetSum) {
console.log(`\nTargetSum: ${targetSum} can't be reached with the given options`)
}
return r
}
const numbers = generate({
targetSum: 88.3,
numberCount: 12,
minNum: 6,
maxNum: 9.99,
})
console.log('number = ', numbers)
console.log(
'Sum = ',
numbers.reduce((p, c) => p + c)
)