// 使用 Math.random() 生成一个 0 到 1 之间的随机数
let randomNum = Math.random();
console.log(randomNum); // 输出类似 0.8972345678912345 的随机数
// 如果需要生成指定范围内的随机数,例如 0 到 10 之间的随机整数
let randomInt = Math.floor(Math.random() * 11); // 乘以 11 是因为包括了 0 和 10
console.log(randomInt); // 输出 0 到 10 之间的一个随机整数
// 如果需要生成指定范围内的随机数,例如 5 到 15 之间的随机整数
let min = 5;
let max = 15;
let randomInRange = Math.floor(Math.random() * (max - min + 1)) + min;
console.log(randomInRange); // 输出 5 到 15 之间的一个随机整数
上一篇:js str_replace
下一篇:js base64解码
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站