سؤال

I was looking at python script when I have across this:

randomNumber = random.randint(2, number)-1

How can I translate this into javascript?

Thanks?

هل كانت مفيدة؟

المحلول

You can use the examples given in the Math.random docs page,

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

console.log(getRandomInt(2, number) - 1);

نصائح أخرى

randomNumber = Math.floor(Math.random() * (1 + number))
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top