Mirgorod Mirgorod - 3 months ago 15
Javascript Question

Generate random number between two numbers in JavaScript

Is there a way to generate a random number in a specified range (e.g. from 1 to 6: 1, 2, 3, 4, 5, or 6) in JavaScript?

Answer

If you wanted to get between 1 and 6, you would calculate:

Math.floor(Math.random() * 6) + 1  

Where:

  • 1 is the start number
  • 6 is the number of possible results (1 + start (6) - end (1))
Comments