Answered by : ugly-unicorn-d69zwa4a7esj
function randomInRange(min, max) { return Math.floor(Math.random() * (max - min) + min);
}
Source : | Last Update : Mon, 20 Sep 21
Answered by : juan-david
var min = 10, max = 25;
//inclusive random (can output 25)
var random = Math.round(Math.random() * (max - min) + min);
//exclusive random (max number that can be output is 24, in this case)
var random = Math.floor(Math.random() * (max - min) + min);
//floor takes the number beneath the generated random and round takes
//which ever is the closest to the decimal
Source : | Last Update : Sun, 23 May 21
Answered by : sore-sandpiper
function getRandomIntInclusive(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min + 1)) + min; //The maximum is inclusive and the minimum is inclusive
}
Source : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random | Last Update : Mon, 03 Feb 20
Answered by : scary-starling-9liataiblhul
function randomInt(min, max) { return Math.floor(Math.random() * (max - min + 1) + min)
}
console.log(randomInt(1, 6)) // random integer between 1 and 6
Source : https://stackoverflow.com/questions/4959975/generate-random-number-between-two-numbers-in-javascript | Last Update : Tue, 20 Jul 21
Answered by : marco
const rnd = (min,max) => { return Math.floor(Math.random() * (max - min + 1) + min) };
Source : | Last Update : Tue, 19 Jan 21
Answered by : sore-sandpiper
function getRandomInt(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
}
Source : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random | Last Update : Mon, 03 Feb 20
Answered by : wiktorkw
function ran(min, max, exclude = false) { let range = max - min + 1; //if you don't want to exclude any number then just returns numbr in range if (!exclude) return Math.floor(Math.random() * range + min); //If you want to exlude only one number then here's O(1) solution. //number from range 1-4 but never get 3: //get number between 1-3 and if its 3 then return 4 if (!exclude?.length) { let num = Math.floor(Math.random() * (range - 1) + min); return num == exclude ? range : num; } //if you want to exlude multiple numbers here's O(n) solution: //foreach number in range i want to check if its not on exlude list //but i dont want to use indexOf or find functions as they are O(n) making this O(n^2) //i create object with keys being exluded numbers as reading object by key is O(1) // ?. operator returns null if such key doesnt have value let badNum = {}; for (let i = 0; i < exclude.length; i++) { badNum[exclude[i]] = true; } let googNum = []; for (let i = 0; i < range; i++) { if (!badNum?.[i]) googNum.push(i); } return googNum[Math.floor(Math.random() * googNum.length)];
}
Source : | Last Update : Thu, 27 Oct 22
Answered by : mahammedi-abdelghani
const randomNumber = ({ min, max } = { min: 0, max: 1 }) => { if (min >= max) { throw Error( `minimum value (${min}) is larger than or equal to maximum value (${max})` ); } return Math.floor(Math.random() * Math.floor(max - min + 1) + min);
};
// Usage: random number between 10 and 100.
const n = randomNumber({ min: 10, max: 100 });
Source : https://gist.github.com/SimonHoiberg/0dc85e01c7c872c3ddc2a409de1232a3 | Last Update : Sat, 21 Aug 21
Answered by : you
function getRandomNumber(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min;
}
// Example usage
var randomNumber = getRandomNumber(1, 10);
console.log(randomNumber);
Source : | Last Update : Mon, 18 Sep 23
Answered by : pi-junky
const rngRange = (min, max) => { return Math.floor(Math.random() * (max - min + 1)) + min;
};
Source : | Last Update : Sun, 05 Mar 23