function randomIntFromInterval(min, max) { // min and max includedreturn Math.floor(Math.random() * (max - min + 1) + min)}
const rndInt = randomIntFromInterval(1, 6)console.log(rndInt)
function rand(min,max,interval){if (typeof(interval)==='undefined') interval = 1;var r = Math.floor(Math.random()*(max-min+interval)/interval);return r*interval+min;}
var a = rand(0,10); //can be 0, 1, 2 (...) 9, 10var b = rand(4,6,0.1); //can be 4.0, 4.1, 4.2 (...) 5.9, 6.0
Generate a 4-bit integer in the range 1-16.If we generated 1, 6, or 11 then output 1.If we generated 2, 7, or 12 then output 2.If we generated 3, 8, or 13 then output 3.If we generated 4, 9, or 14 then output 4.If we generated 5, 10, or 15 then output 5.If we generated 16 then reject it and try again.
function getR(lower, upper) {
var percent = (Math.random() * 100);// this will return number between 0-99 because Math.random returns decimal number from 0-0.9929292 something like that//now you have a percentage, use it find out the number between your INTERVAL :upper-lowervar num = ((percent * (upper - lower) / 100));//num will now have a number that falls in your INTERVAL simple mathsnum += lower;//add lower to make it fall in your INTERVAL//but num is still in decimal//use Math.floor>downward to its nearest integer you won't get upper value ever//use Math.ceil>upward to its nearest integer upper value is possible//Math.round>to its nearest integer 2.4>2 2.5>3 both lower and upper value possibleconsole.log(Math.floor(num), Math.ceil(num), Math.round(num));}
randomNumber(-2, 3); // can be -2, -1, 0, 1, 2 and 3randomNumber(-5, -2); // can be -5, -4, -3 and -2randomNumber(0, 4); // can be 0, 1, 2, 3 and 4randomNumber(4, 0); // can be 0, 1, 2, 3 and 4
function generateRangom(low, up) {const u = Math.max(low, up);const l = Math.min(low, up);const diff = u - l;const r = Math.floor(Math.random() * (diff + 1)); //'+1' because Math.random() returns 0..0.99, it does not include 'diff' value, so we do +1, so 'diff + 1' won't be included, but just 'diff' value will be.
return l + r; //add the random number that was selected within distance between low and up to the lower limit.}
Java解决方案:
public static int generateRandom(int low, int up) {int l = Math.min(low, up);int u = Math.max(low, up);int diff = u - l;
int r = (int) Math.floor(Math.random() * (diff + 1)); // '+1' because Math.random() returns 0..0.99, it does not include 'diff' value, so we do +1, so 'diff + 1' won't be included, but just 'diff' value will be.
return l + r;//add the random number that was selected within distance between low and up to the lower limit.}
function randomInteger(pMin = 1, pMax = 1_000_000_000)//Author: Axel Gauffre.//Here: https://stackoverflow.com/a/74636954/5171000//Inspired by: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random#getting_a_random_number_between_two_values////This function RETURNS A RANDOM INTEGER between pMin (INCLUDED) and pMax (INCLUDED).// - pMin and pMax should be integers.// - HOWEVER, if pMin and/or pMax are FLOATS, they will be ROUNDED to the NEAREST integer.// - NEGATIVE values ARE supported.// - The ORDER of the 2 arguments has NO consequence: If pMin > pMax, then pMin and pMax will simply be SWAPPED.// - If pMin is omitted, it will DEFAULT TO 1.// - If pMax is omitted, it will DEFAULT TO 1 BILLION.////This function works in ANY cases (fully tested).//Also, the distribution of the results has been fully tested and is 100% correct.{pMin = Math.round(pMin);pMax = Math.round(pMax);if (pMax < pMin) { let t = pMin; pMin = pMax; pMax = t;}return Math.floor(Math.random() * (pMax+1 - pMin) + pMin);}