Generate Random Numbers in JavaScript
Category Programming Techniques
JavaScript can use the Math (arithmetic) object to generate random numbers.
Math Object Methods to Know
Method | Description |
---|---|
ceil(x) | Rounds a number up to the nearest whole number. |
round(x) | Rounds a number to the nearest whole number. |
random() | Returns a random number between 0 and 1, inclusive of 0 and exclusive of 1. |
Some examples are as follows:
Math.ceil(Math.random()*10); // Gets a random integer from 1 to 10, with a very small probability of getting 0.
Math.round(Math.random()); // Can evenly obtain a random integer from 0 to 1.
Math.floor(Math.random()*10); // Can evenly obtain a random integer from 0 to 9.
Math.round(Math.random()*10); // Can almost evenly obtain a random integer from 0 to 10, with the chances of getting the minimum value 0 and the maximum value 10 being half as likely.
Because the results are 0 for 0~0.4, 1 for 0.5 to 1.4, 9 for 8.5 to 9.4, and 10 for 9.5 to 9.9. So the distribution intervals at the beginning and end are only half of the other numbers.
Generating Random Integers in the Range [n,m]
Functionality: Generates a random integer within the range [n,m]
.
This is very useful in JavaScript when generating captcha codes or randomly selecting an option.
// Generates a random number from minNum to maxNum
function randomNum(minNum,maxNum){
switch(arguments.length){
case 1:
return parseInt(Math.random()*minNum+1,10);
break;
case 2:
return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10);
break;
default:
return 0;
break;
}
}
Process Analysis:
Math.random() generates numbers in the range [0,1), so Math.random()*5 generates numbers in the range {0,5).
Typically, we want to get an integer, so we need to process the result.
parseInt(), Math.floor(), Math.ceil(), and Math.round() can all be used to get integers.
parseInt() and Math.floor() both result in rounding down.
So numbers generated by Math.random()*5 are all random integers in the range [0,4].
So to generate a random number in the range [1,max], the formula is as follows:
// max - the expected maximum value
parseInt(Math.random()*max,10)+1;
Math.floor(Math.random()*max)+1;
Math.ceil(Math.random()*max);
So to generate a random number from [0,max] to any number, the formula is as follows:
// max - the expected maximum value
parseInt(Math.random()*(max+1),10);
Math.floor(Math.random()*(max+1));
So if you want to generate a random number in the range [min,max], the formula is as follows:
// max - the expected maximum value
// min - the expected minimum value
parseInt(Math.random()*(max-min+1)+min,10);
Math.floor(Math.random()*(max-min+1)+min);
Example
// User input
const min = parseInt(prompt("Enter the minimum value: "));
const max = parseInt(prompt("Enter the maximum value: "));
// Generate a random number
const a = Math.floor(Math.random() * (max - min + 1)) + min;
// Display the random number
document.write(`${min} to ${max} random number: ${a}`);
An online random number generator: https://c.tutorialpro.org/front-end/6680/
>
Original article link: http://www.cnblogs.com/starof/p/4988516.html
**Click to Share Notes
-
-
-