Q:

random int javascript

function randomIntFromInterval(min, max) { // min and max included 
  return Math.floor(Math.random() * (max - min + 1) + min);
}
5
Math.floor(Math.random() * 10);
11

function getRndInteger(min, max) {

    return Math.floor(Math.random() * (max - min + 1) ) + min;

}
 
1
function randomInteger(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}
4
function randomNumber(min, max) {
  return Math.random() * (max - min) + min;
}
4
/**
* Gets random int
* @param min 
* @param max 
* @returns random int - min & max inclusive
*/
getRandomInt(min, max) : number{
	min = Math.ceil(min);
	max = Math.floor(max);
	return Math.floor(Math.random() * (max - min + 1)) + min; 
}
1
let int = Math.floor(Math.random() * 10);
1
//Returns random Int between 0 and 2 (included)
Math.floor(Math.random()*3)
1
function random(min, max) {
  return ~~(Math.random() * (max - min + 1) + min);
}
random(1, 5);
3
Math.floor(Math.random() * 3);
5
function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
}
1
/* If 1 argument is given, minimum will be set to 0 and maximum to this argument
 * If 2 arguments were given, the fist would be the minimum and the second the maximum
 * The function will return an integer in [min, max[
 */
const Math.randint = function (min,max) {
  [min,max] = (max===undefined)?[0,min]:(min>max)[max,min]:[min,max];
  return Math.floor(Math.random*(max-min)+min);
}
4

New to Communities?

Join the community