//To genereate a number between 0-1Math.random();//To generate a number that is a whole number rounded downMath.floor(Math.random())/*To generate a number that is a whole number rounded down between
1 and 10 */Math.floor(Math.random()*10)+1//the + 1 makes it so its not 0.
Math.random()// will return a number between 0 and 1, you can then time it up to get larger numbers.//When using bigger numbers remember to use Math.floor if you want it to be a integerMath.floor(Math.random()*10)// Will return a integer between 0 and 9Math.floor(Math.random()*11)// Will return a integer between 0 and 10// You can make functions aswell functionrandomNum(min, max){returnMath.floor(Math.random()*(max - min))+ min;// You can remove the Math.floor if you don't want it to be an integer}
// Returns an integer between min and max (the maximum is exclusive and the minimum is inclusive)functiongetRandomInt(min, max){
min =Math.ceil(min);
max =Math.floor(max);returnMath.floor(Math.random()*(max - min)+ min);}
/*
The Math.random() function returns a floating-point, pseudo-random
number in the range 0 to less than 1 (inclusive of 0, but not 1)
with approximately uniform distribution over that range — which you
can then scale to your desired range. The implementation selects the
initial seed to the random number generation algorithm; it cannot
be chosen or reset by the user.
*/functiongetRandomInt(max){returnMath.floor(Math.random()*Math.floor(max));}console.log(getRandomInt(3));// expected output: 0, 1 or 2console.log(getRandomInt(1));// expected output: 0console.log(Math.random());// expected output: a number from 0 to <1
// min value of the random numbervar min =5;// max value of the random numbervar max =25;// generate the random numbervar rdm =(Math.random()*(max - min))+ min
// generate the random number without "."var rdm =Math.round((Math.random()*(max - min))+ min)
/**
* Returns a random number between min (inclusive) and max (exclusive)
*/functiongetRandomArbitrary(min, max){returnMath.random()*(max - min)+ min;}/**
* Returns a random integer between min (inclusive) and max (inclusive).
* The value is no lower than min (or the next integer greater than min
* if min isn't an integer) and no greater than max (or the next integer
* lower than max if max isn't an integer).
* Using Math.round() will give you a non-uniform distribution!
*/functiongetRandomInt(min, max){
min =Math.ceil(min);
max =Math.floor(max);returnMath.floor(Math.random()*(max - min +1))+ min;}
Math.random()// Or something between 0 and 9:Math.floor(Math.random()*10)// You can even make functions:functionrandom(min, max){returnMath.floor(Math.random()*(max - min))+ min}
Math.random()// Returns number from 0 to 1. Math.floor(Math.random()* max)// Return number from 0 to max minus 1// ExampleMath.floor(Math.random()*10)// Return number from 0 to 9// Add a +1 to return a number from 0 to maxMath.floor(Math.random()*10)+1// Returns Number from 1 to 10// To have a min and max Math.floor(Math.random()*(max - min))+ min
// ExampleMath.floor(Math.random()*(20-10))+10
let randomNum =Math.floor(Math.random()*5)return(0 or 1 or 2 or 3 or 4)let randomNum =Math.floor(Math.random()*5)+1return(1 or 2 or 3 or 4)// * 5 in this code meaning a number between 0 and 4
/*
`Math.random` returns a pseudo-random number between 0 and 1.
a pseudo-random number is generated by an algorithm, it is not
technically actually random, but for all intents and purposes
it is random enough that no human should be able to find a
pattern
*/Math.random();// -> Decimal number between 0 and 1Math.round(Math.random());// -> 0 or 1Math.random()* max;// -> Decimal number between 0 and maxMath.floor(Math.random()* max);// -> Whole number between 0 and max - 1Math.round(Math.random()* max);// -> Whole number between 0 and maxMath.ceil(Math.random()* max);// -> Whole number between 1 and max(Math.random()*(max - min))+ min;// Decimal number between min and maxMath.floor((Math.random()*(max - min))+ min);// Whole number between min and max - 1Math.round((Math.random()*(max - min))+ min);// Whole number between min and maxMath.ceil((Math.random()*(max - min))+ min);// Whole number between min + 1 and maxMath.random()*Math.random();// Decimal number between 0 and 1 with a tendency to be smaller1-Math.random()*Math.random();// Decimal number between 0 and 1 with a tendency to be larger
/*
To generate a random number between a and b keep in mind this formula:
a-> minimum value
b->maximum value */let randomNumber = a+(b-a)*Math.random();/*this gives you any random number between a and b
*/
var random =Math.floor(Math.random()*10)+1;//This generates a number between 10 and 1
example.children[random].innerHTML= random;//This displays the number on screen
functiongetRandomIntInclusive(min, max){
min =Math.ceil(min);
max =Math.floor(max);returnMath.floor(Math.random()*(max - min +1))+ min;// max & min both included }
var randomNumberBetween0and19 =Math.floor(Math.random()*20);functionrandomWholeNum(){// Only change code below this line.returnMath.floor(Math.random()*10);}
// You can make functions aswell functionrandomNum(min, max){returnMath.floor(Math.random()*(max - min))+ min;// You can remove the Math.floor if you don't want it to be an integer}
var options =["Your options","Another option!","This is an option."];var chosenOption =Math.floor(Math.random()* options.length);console.log(options[option]);
Math.floor(Math.random()*10)+1// Random number Between 1 and 10// First Math.random give us a random number between 0 and 0,99999// The we multiply it by 10// And we round dow with Math.floor// We add 1 so the result will never be 0 // Another Example:
h.floor(Math.random()*20)+10// Random number Between 10 and 20
//returns a random number between 1 and maxNumber//replace Math.ceil with Math.round if you want to include 0 as wellconstrandomNumber=(maxNumber)=>{returnMath.ceil(Math.random()* maxNumber);}
// using array and random examplefunctiongetTheBill(){let names =["bob","mike","matt"];let randomFriend =Math.floor(Math.random()* names.length);let randomFriendBuyng = names[randomFriend];return randomFriendBuyng +" is buying us lunch today!";}
// Math.random()// The Math.random() function returns a floating-point, pseudo-random number that's greater than or equal to 0 and less than 1, with approximately uniform distribution over that range — which you can then scale to your desired range.// parameter inside Math.random () is scale to desired range.// EXAMPLE :1 let ran_num =Math.random();console.log(ran_num);// OUTPUT: It gives any floating point number between 0 to 1 on every refresh of code like:// 0.439905306460564// 0.24377528882567256// 0.5522415279920441 etc...... // EXAMPLE :2 let ran_num_2 =(Math.random()*10);console.log(ran_num_2);// OUTPUT: It gives any floating point number between 0 to 10 on every refresh of code like:// 0.6199449585914163// 2.532543541582639// 1.9219543257575755 etc...... // EXAMPLE :1 let ran_num_3 =Math.floor(Math.random()*10);console.log(ran_num_3);// OUTPUT: It gives any floating point number between 0 to 10 and in integer(WITH OUT DECIMAL POINT) on every refresh of code like:// 4// 9// 1 etc......
var array =["one","two","three","four","five"],
result = array.slice(0,3).map(function(){returnthis.splice(Math.floor(Math.random()*this.length),1)[0];}, array.slice());console.log(result);Run code snippet