SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2009
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Random Number Generation.

    I am using

    Math.floor(Math.random() * high) + low;
    to generate a random number. I thought this will generate a random value between the high value and low value. But when I use it in the below program the number generated is sometimes greater than the high value. Can someone please tell me why?


    <html>
    <head>
    <title>The Number Guessing Game</title>
    <script>
    //Dipin
    //The Number Guessing Game
    //The computer will the guess the number that the use thinks

    var guess = 0;
    var reply = "";
    var msg = "";
    var high = 100;
    var low = 1;

    msg = "Please think of a number between 1 and 100. \n";
    msg += "I will try to guess the number you thought. \n";
    msg += "If the number I guess is too high please reply with a 'h'. \n";
    msg += "If the number I guess is too low please reply with a 'l'. \n";
    msg += "If I guess correctly please reply with a 'c'. \n";
    alert(msg);

    while(reply != 'c'){
    guess = Math.floor(Math.random() * high) + low;

    alert("low: "+low); //testing statement
    alert("high: "+high); //testing statement
    //alert("guess: "+guess); //testing statement

    msg = "My guess is: "+guess+". \n";
    msg += "Please reply with h=high/l=low/c=correct. \n";
    reply = prompt(msg,"");

    if(reply == 'h'){
    high = guess;
    } //too high condition
    if(reply == 'l'){
    low = guess;
    } //too low condition
    if(reply == 'c'){
    alert("Hooray!!! I am the greatest guesser.");
    } //correct guess condition
    } //end while loop
    </script>
    </head>
    <body>
    <center>
    <h2>The Number Guessing Game</h2>
    </center>
    <hr>
    <h3>Please refresh the page to play the game again.</h3>
    </body>
    </html>

  2. #2
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code:
    var range = high - low;
    var rndnum = Math.floor(Math.random() * range) + low;
    Ian Anderson
    www.siteguru.co.uk

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2009
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you. That seems to work perfectly.

  4. #4
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by siteguru View Post
    Code:
    var range = high - low;
    var rndnum = Math.floor(Math.random() * range) + low;
    Notice that the code gives a number from low to high but not including high. For example, if low is 1 and high is 10, it will give random whole numbers from 1 to 9 inclusive. 10 is not included.

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,863
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    If you want it to include high as one of the values then simply use

    var range = high - low + 1;
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •