首页 > 其他分享 >js: Math.random()获取随机数

js: Math.random()获取随机数

时间:2023-02-03 17:33:02浏览次数:52  
标签:console min getRandomInt max random js Math

文档

获取一个0-1之间的随机数

// [0, 1)
console.log(Math.random());
// 0.7532061893284896

一个文档给出的示例

得到一个两数之间的随机整数

// [min, max)
function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    // 不含最大值,含最小值
    return Math.floor(Math.random() * (max - min)) + min; 
}

console.log(getRandomInt(10, 100));
// 61

标签:console,min,getRandomInt,max,random,js,Math
From: https://blog.51cto.com/mouday/6035953

相关文章