Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Using Math.random and "if, else if, else"

    I tried writing a program using Math.random and if, else if and else...
    This is what I wrote:

    var co1 = Math.random(1, 5);
    var co2 = Math.random(1, 5);
    if (co1 > co2){
    console.log("1 wins!");
    }
    else if (co2 > co1){
    console.log("2 wins!");
    }
    else co2 === co1;
    console.log("TIE!")



    Now when I run it, I always get "1 wins!" or "2 wins!" and a "TIE!".
    for example:
    Running:
    1 wins!
    TIE!
    Running:
    2 wins!
    TIE!

    Why is this happening? And how could I stop the Tie from being written without deleting it completly from the program?

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by Collinar View Post
    This is what I wrote:

    var co1 = Math.random(1, 5);
    var co2 = Math.random(1, 5);
    What documentation did you read to get the usage of Math.random ?

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    Code:
    <script type = "text/javascript">
    
    var co1 = Math.ceil(Math.random() *5);  //  integer 1-5
    var co2 = Math.ceil(Math.random() *5);
    if (co1 > co2) {
    alert ("1 wins!");
    }
    else if (co2 > co1){
    alert ("2 wins!"); 
    }
    else if (co2 == co1)  {
    alert ("TIE!");
    }
    
    </script>
    console.log("TIE!") says do just that. It is not contained within an if/else clause.


    When the questions are being asked, that's when you come up with the answers.
    Last edited by Philip M; 11-10-2012 at 03:24 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    This is wrong, Philip:
    Code:
    var co1 = Math.ceil(Math.random() *5);
    There is a vanishingly small but still finite chance that Math.random() can return a value of ZERO.

    And so on those (admiittedly very rare) occasions when it does so, Math.ceil(0) is 0 and you get a number outside the desired range.

    To avoid even a *chance* of that happening you really should do
    Code:
    var co1 = 1 + Math.floor(Math.random() * 5);
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    Quote Originally Posted by Old Pedant View Post
    This is wrong, Philip:
    Code:
    var co1 = Math.ceil(Math.random() *5);
    There is a vanishingly small but still finite chance that Math.random() can return a value of ZERO.

    And so on those (admiittedly very rare) occasions when it does so, Math.ceil(0) is 0 and you get a number outside the desired range.

    To avoid even a *chance* of that happening you really should do
    Code:
    var co1 = 1 + Math.floor(Math.random() * 5);
    Yes, you are quite right. As you say, a vanishingly small chance but still possible to return zero. But that would not affect this particular script, even if zero came up.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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