Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

how to use Math.random() and Math.floor() in javascript to generate a random number between 30 and 90 ?

3 Answers

Relevance
  • ?
    Lv 7
    9 years ago
    Favorite Answer

    https://developer.mozilla.org/en-US/docs/JavaScrip...

    // Returns a random integer between min and max

    // Using Math.round() will give you a non-uniform distribution!

    function getRandomInt(min, max) {

        return Math.floor(Math.random() * (max - min + 1)) + min;

    }

    - Dominic

  • 9 years ago

    Is it a must that you use floor()? Why not generate a random between 0 and 60, then just add 30 to it?

  • 9 years ago

    30+Math.floor(Math.random()*61)

    ^--- That is for 30 and 90 inclusive.

Still have questions? Get your answers by asking now.