Javascript - 生成一个范围内的随机小区间
Javascript - Generate random small intervals in a range
我正在尝试生成范围之间的随机间隔:2,5k - 10M。
目前,我正在做以下事情:
const MIN_NUMBER = 2500;
const MAX_NUMBER = 10000000;
const random = (min, max, floating = false) => {
const result = Math.random() * max + min;
return floating ? result : Math.floor(result);
};
const min = random(MIN_NUMBER, MAX_NUMBER / 10);
const max = random(min, min * 10);
const interval = `[${min}, ${max}]`;
console.log(interval);
但是可以看到,生成的区间是small/medium的概率不是很高
我想获得随机间隔,例如:
- [2500, 10400]
- [2500, 9919]
- [3000000, 3301029]
- [500000, 611223]
我没有遵循任何特定规则,但如您所见,与
有关
- [2500, 400000]
- [2500, 71000]
- [3000000, 10000000]
- [500000, 3120000]
它们被认为是“small/medium”,因为最大值和最小值之间没有“非常大”的差异。
使用我当前的算法,您可以检查生成的平均差异是否很高:
const MIN_NUMBER_OF_LIKES = 2500;
const MAX_NUMBER_OF_LIKES = 10000000;
const random = (min, max, floating = false) => {
const result = Math.random() * max + min;
return floating ? result : Math.floor(result);
};
let averageDiff = 0;
const numIterations = 1000;
for (let i = 0; i < numIterations; i++) {
const min = random(MIN_NUMBER_OF_LIKES, MAX_NUMBER_OF_LIKES / 10);
const max = random(min, min * 10);
averageDiff += max - min;
}
averageDiff /= numIterations;
console.log({ averageDiff });
如何获取随机的小片段?
--注意:随机生成的间隔之间的差异也是随机的,但它必须是“small/medium”(不像我当前的解决方案那么大)。
首先在您希望的大小范围内随机选择间隔大小如何 - 这样您就可以得到 INTERVAL_SIZE 的值。作为第二步,您随机找到 MIN_NUMBER_OF_LIKES 和 MAX_NUMBER_OF_LIKES 之间的最小值 - INTERVAL,这样您就可以得到 STARTPOINT。
所以最终的 INTERVAL 将有 STARTPOINT 和 ENDPOINT = STARTPOINT + INTERVAL_SIZE
基于@krnz 解决方案:
const MIN = 2500;
const MAX = 10000000;
const random = (min, max, floating = false) => {
const result = Math.random() * max + min;
return floating ? result : Math.floor(result);
};
function generateRandomSmallInterval() {
const intervalSize = random(1000, 10000);
const start = random(MIN, MAX-intervalSize);
const end = start + intervalSize;
return {start, end};
}
const interval = generateRandomSmallInterval();
console.log({ interval });
console.log(`Diff: ${interval.end - interval.start}`);
使用 lodash 重构和通用化代码:
import { random } from 'lodash';
function generateRandomIntervalInRange(min, max, maxIntervalSize = max - min) {
if (max - min <= 0) {
throw new Error("The `max` argument must be greater than `min`.");
}
if (maxIntervalSize <= 0) {
throw new Error("The maximum interval size must be greater than 0.");
}
if (maxIntervalSize > max - min) {
throw new Error(
`The maximum interval size mustn't be greater than ${max - min}.`
);
}
const intervalSize = random(1, maxIntervalSize);
const start = random(min, max - intervalSize);
const end = start + intervalSize;
return { start, end };
}
//
// MAIN
//
const MIN = 1;
const MAX = 10;
const MAX_INTERVAL_SIZE = 5;
console.log(generateRandomIntervalInRange(MIN, MAX, MAX_INTERVAL_SIZE));
我正在尝试生成范围之间的随机间隔:2,5k - 10M。
目前,我正在做以下事情:
const MIN_NUMBER = 2500;
const MAX_NUMBER = 10000000;
const random = (min, max, floating = false) => {
const result = Math.random() * max + min;
return floating ? result : Math.floor(result);
};
const min = random(MIN_NUMBER, MAX_NUMBER / 10);
const max = random(min, min * 10);
const interval = `[${min}, ${max}]`;
console.log(interval);
但是可以看到,生成的区间是small/medium的概率不是很高
我想获得随机间隔,例如:
- [2500, 10400]
- [2500, 9919]
- [3000000, 3301029]
- [500000, 611223]
我没有遵循任何特定规则,但如您所见,与
有关- [2500, 400000]
- [2500, 71000]
- [3000000, 10000000]
- [500000, 3120000]
它们被认为是“small/medium”,因为最大值和最小值之间没有“非常大”的差异。
使用我当前的算法,您可以检查生成的平均差异是否很高:
const MIN_NUMBER_OF_LIKES = 2500;
const MAX_NUMBER_OF_LIKES = 10000000;
const random = (min, max, floating = false) => {
const result = Math.random() * max + min;
return floating ? result : Math.floor(result);
};
let averageDiff = 0;
const numIterations = 1000;
for (let i = 0; i < numIterations; i++) {
const min = random(MIN_NUMBER_OF_LIKES, MAX_NUMBER_OF_LIKES / 10);
const max = random(min, min * 10);
averageDiff += max - min;
}
averageDiff /= numIterations;
console.log({ averageDiff });
如何获取随机的小片段?
--注意:随机生成的间隔之间的差异也是随机的,但它必须是“small/medium”(不像我当前的解决方案那么大)。
首先在您希望的大小范围内随机选择间隔大小如何 - 这样您就可以得到 INTERVAL_SIZE 的值。作为第二步,您随机找到 MIN_NUMBER_OF_LIKES 和 MAX_NUMBER_OF_LIKES 之间的最小值 - INTERVAL,这样您就可以得到 STARTPOINT。 所以最终的 INTERVAL 将有 STARTPOINT 和 ENDPOINT = STARTPOINT + INTERVAL_SIZE
基于@krnz 解决方案:
const MIN = 2500;
const MAX = 10000000;
const random = (min, max, floating = false) => {
const result = Math.random() * max + min;
return floating ? result : Math.floor(result);
};
function generateRandomSmallInterval() {
const intervalSize = random(1000, 10000);
const start = random(MIN, MAX-intervalSize);
const end = start + intervalSize;
return {start, end};
}
const interval = generateRandomSmallInterval();
console.log({ interval });
console.log(`Diff: ${interval.end - interval.start}`);
使用 lodash 重构和通用化代码:
import { random } from 'lodash';
function generateRandomIntervalInRange(min, max, maxIntervalSize = max - min) {
if (max - min <= 0) {
throw new Error("The `max` argument must be greater than `min`.");
}
if (maxIntervalSize <= 0) {
throw new Error("The maximum interval size must be greater than 0.");
}
if (maxIntervalSize > max - min) {
throw new Error(
`The maximum interval size mustn't be greater than ${max - min}.`
);
}
const intervalSize = random(1, maxIntervalSize);
const start = random(min, max - intervalSize);
const end = start + intervalSize;
return { start, end };
}
//
// MAIN
//
const MIN = 1;
const MAX = 10;
const MAX_INTERVAL_SIZE = 5;
console.log(generateRandomIntervalInRange(MIN, MAX, MAX_INTERVAL_SIZE));