# javascript - 范围随机数 - 获取随机数

## 在JavaScript中生成两个数字之间的随机数 (12)

Math.random（）函数在[0，1）范围内返回一个浮点伪随机数; 即从0（包括）到1（不包括）

# 案例0

`min` + 0 *（ `max` - `min` ）= 分钟

# 情况1

`min` + 1 *（ `max` - `min` ）= 最大

# 使用Math.random 0 <= r <1的随机情况

`min` + r *（ `max` - `min` ）= X ，其中X具有min <= X < max的范围

``````function generateRandomInteger(min, max) {
return Math.floor(min + Math.random()*(max+1 - min))
}
``````

# 获得随机数

`generateRandomInteger(-20, 20)` ;

# Math.random()

Math.random()开发者网络文档：

``````// Returns a random integer between min (included) and max (included)

function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
``````

``````// 0 -> 10
Math.floor(Math.random() * 11);

// 1 -> 10
Math.floor(Math.random() * 10) + 1;

// 5 -> 20
Math.floor(Math.random() * 16) + 5;

// -10 -> (-2)
Math.floor(Math.random() * 9) - 10;
``````

``````Math.floor((Math.random() * 10) + 1);
``````

-

`_.random(min, max)`

• `(Math.random() * 6 | 0) + 1`
• `~~(Math.random() * 6) + 1`

``````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-lower
var num = ((percent * (upper - lower) / 100));
//num will now have a number that falls in your INTERVAL simple maths
num += 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 possible
console.log(Math.floor(num), Math.ceil(num), Math.round(num));
}
``````

``````function randInt(min, max) {
var MAX_UINT32 = 0xFFFFFFFF;
var range = max - min;

if (!(range <= MAX_UINT32)) {
throw new Error(
"Range of " + range + " covering " + min + " to " + max + " is > " +
MAX_UINT32 + ".");
} else if (min === max) {
return min;
} else if (!(max > min)) {
throw new Error("max (" + max + ") must be >= min (" + min + ").");
}

// We need to cut off values greater than this to avoid bias in distribution
// over the range.
var maxUnbiased = MAX_UINT32 - ((MAX_UINT32 + 1) % (range + 1));

var rand;
do {
rand = crypto.getRandomValues(new Uint32Array(1))[0];
} while (rand > maxUnbiased);

var offset = rand % (range + 1);
return min + offset;
}

console.log(randInt(-8, 8));          // -2
console.log(randInt(0, 0));           // 0
console.log(randInt(0, 0xFFFFFFFF));  // 944450079
console.log(randInt(-1, 0xFFFFFFFF));
// Uncaught Error: Range of 4294967296 covering -1 to 4294967295 is > 4294967295.
console.log(new Array(24).fill().map(n => randInt(8, 12)));
// [11, 8, 8, 11, 10, 8, 8, 12, 12, 12, 9, 9,
//  11, 8, 11, 8, 8, 8, 11, 9, 10, 12, 9, 11]
console.log(randInt(10, 8));
// Uncaught Error: max (8) must be >= min (10).``````

``````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, 10
var b = rand(4,6,0.1); //can be 4.0, 4.1, 4.2 (...) 5.9, 6.0
``````

``````function randomInt(min,max)
{
return Math.floor(Math.random()*(max-(min+1))+(min+1));
}
``````

``````_.random(min, max)
``````

``````function randomBetween(min, max) {
if (min < 0) {
return min + Math.random() * (Math.abs(min)+max);
}else {
return min + Math.random() * max;
}
}
``````

``````randomBetween(-10,15)//or..
randomBetween(10,20)//or...
randomBetween(-200,-100)
``````

``````function randomIntFromInterval(min,max)
{
return Math.floor(Math.random()*(max-min+1)+min);
}
``````

``````var x = 6; // can be any number
var rand = Math.floor(Math.random()*x) + 1;
``````