使用JavaScript(2)使用Math对象的random() 方法编制-个产生0~ 100之间(含0、100 )的随机整数的函数。

以下是使用JavaScript编写的函数,用于生成0到100之间的随机整数:

function getRandomInt() {
  return Math.floor(Math.random() * 101);
}

console.log(getRandomInt()); // 输出一个0到100之间的随机整数

使用Math.random()方法可以生成0到1之间的随机小数,将其乘以101后取整,即可得到0到100之间的随机整数。

猜你喜欢

转载自blog.csdn.net/m0_74265396/article/details/135435477
今日推荐