// 使用 Math.random() 生成一个 0 到 1 之间的随机数(不包括 1)
let randomNum = Math.random();
console.log(randomNum);
// 如果需要生成指定范围内的随机数,例如 0 到 10 之间(不包括 10)
let randomNumInRange = Math.random() * 10;
console.log(randomNumInRange);
// 如果需要生成指定范围内的整数随机数,例如 1 到 10 之间(包括 1 和 10)
let randomIntBetween1And10 = Math.floor(Math.random() * 10) + 1;
console.log(randomIntBetween1And10);
Math.random() 返回一个 0 到 1 之间的随机浮点数(不包括 1)。Math.random() 的结果乘以该范围的大小。Math.floor() 或 Math.ceil() 来取整。上一篇:js datetime
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站