-5

i am confused about Math.Random() in javascript in know that this function will return a number between 0 and 1

i am trying to return a number between 400 and 500 and what i did is this :

var y = Math.floor((Math.random() * 400) + 100);

but this code may return a value of 100 or 200 what is the best practice to create a number where the minimum is n0 and maximum is n1

Sora
  • 2,465
  • 18
  • 73
  • 146
  • 1
    Your should take a look at this question : http://stackoverflow.com/questions/1527803/generating-random-numbers-in-javascript-in-a-specific-range – Supamiu Aug 15 '15 at 11:23
  • 2
    is it 500 or 480? your question is contradicting – thumbmunkeys Aug 15 '15 at 11:24
  • MDN has [example functions that do what you want](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random#Using_Math.random()). – nnnnnn Aug 15 '15 at 11:25
  • Math.floor((Math.random() * (80 + 1)) + 400) – M22 Sep 22 '22 at 13:15

1 Answers1

7

try this:

var y = Math.floor((Math.random() * (80 +1)) + 400);

Math.random() * 80 will yield a number between 0 and 80, then just add 400 to it

thumbmunkeys
  • 20,606
  • 8
  • 62
  • 110