1function getRandomInt(min, max) {
2 min = Math.ceil(min);
3 max = Math.floor(max);
4 return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
5}
6
1const randomArrayInRange = (min, max, n) => Array.from({ length: n }, () => Math.floor(Math.random() * (max - min + 1)) + min);
2
3// Example
4randomArrayInRange(1, 100, 10);
1const randomNumber = ({ min, max } = { min: 0, max: 1 }) => {
2 if (min >= max) {
3 throw Error(
4 `minimum value (${min}) is larger than or equal to maximum value (${max})`
5 );
6 }
7
8 return Math.floor(Math.random() * Math.floor(max - min + 1) + min);
9};
10
11// Usage: random number between 10 and 100.
12const n = randomNumber({ min: 10, max: 100 });