以下是一个JavaScript函数,可以生成1到10之间的随机整数:

function getRandomInt(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

var randomInt = getRandomInt(1, 10);
console.log(randomInt);

这个函数使用Math.random()方法生成一个0到1之间的随机小数,然后将其乘以max - min + 1,再加上min,最后使用Math.floor()函数将结果向下取整,得到一个1到10之间的整数。

js随机1-10选一个整数

原文地址: https://www.cveoy.top/t/topic/hUw7 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录