3

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?

Progo
  • 3,452
  • 5
  • 27
  • 44
  • @p.s.w.g I don't think so because I am trying to translate this specific code. – Progo Mar 01 '14 at 17:31
  • 1
    @Progo It’s exactly what your Python code does though… – poke Mar 01 '14 at 17:32
  • @poke But I already know how to get a random integer in javascript. I just wanted to know how to translate this python code because I don't program in python. – Progo Mar 01 '14 at 17:33
  • @Progo Then it sounds like your real question is, "What does this bit of Python do?" See http://docs.python.org/2/library/random.html#random.randint – p.s.w.g Mar 01 '14 at 17:36
  • Googling for “random.randint python” gives exactly that link btw. – poke Mar 01 '14 at 17:36
  • Just so that you know. Python's randint method isn't truely random. – Nobel Chicken Jun 16 '14 at 03:21

2 Answers2

2

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);
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
1
randomNumber = Math.floor(Math.random() * (1 + number))
Z .
  • 12,657
  • 1
  • 31
  • 56