能用js写一个随机数吗
可以,以下为示例代码:
// 生成一个0-9的随机整数
var randomNum = Math.floor(Math.random() * 10);
console.log(randomNum);
// 生成一个1-6的随机整数,模拟掷骰子
var randomNum = Math.floor(Math.random() * 6) + 1;
console.log(randomNum);
// 生成一个指定范围内的随机整数,例如1-100
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var randomNum = getRandomInt(1, 100);
console.log(randomNum);
原文地址: http://www.cveoy.top/t/topic/cgLy 著作权归作者所有。请勿转载和采集!