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

Thread: Random.Range

  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Random.Range

    I have looked over the internet and what I found never works for me I want a random range from a number to a number here is what i tried.
    Code:
    var value = Math.floor(Math.random() * (nb - na + 1) + na)

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    What is a random range?

    So you want to have a range that has a random start number and a random end number and contains every number in between?

    The code you showed gives you one(!) random number between (and including) na and nb

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Code:
    <script type = "text/javascript">
    function random(lo,hi) {
    return lo + Math.floor((hi-lo + 1) * Math.random() );
    }
    
    alert (random (20,40));
    
    </script>
    If you wish the hi and lo values to be random themselves, you must start by generating them within some desired range.

    Code:
    var randy1 = 0;
    var randy2 = 0;
    var num  = 100;
    while (randy1 == randy2) {  // not both the same number!
    var randy1 = Math.floor(Math.random() * num +1);  // generates 1-100 truly randomly
    var randy2 = Math.floor(Math.random() * num +1);  // generates 1-100 truly randomly
    }
    
    if (randy1 > randy2) {
    hi = randy1;
    lo = randy2;
    }
    if (randy2 > randy1) {
    hi = randy2;
    lo = randy1;
    }
    alert (lo + "  " + hi);
    alert (random(lo,hi));
    Man Pushes Melon Through ex-Partner's Catflap. - headline in Norwich Evening News
    Last edited by Philip M; 07-13-2012 at 10:20 AM.

    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 Coder
    Join Date
    Jul 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If its low 5 and high 6 it gives 1 though?
    function roltd(low,high){

    var value = Math.floor(Math.random() * (high - low + 1) + low);

    document.getElementById("page").innerHTML+= value +"<br/>";
    }
    Last edited by Tim Conner; 07-13-2012 at 03:43 PM.

  • #5
    New Coder
    Join Date
    Jul 2012
    Location
    Ukraine
    Posts
    71
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Quote Originally Posted by Tim Conner View Post
    If its low 5 and high 6 it gives 1 though?
    function roltd(low,high){

    var value = Math.floor(Math.random() * (high - low + 1) + low);

    document.getElementById("page").innerHTML+= value +"<br/>";
    }
    Do you mean that roltd(5, 6) writes 1?

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by Tim Conner View Post
    If its low 5 and high 6 it gives 1 though?
    Nonsense.


    Code:
    <script type = "text/javascript">
    function roltd(low,high){
    return Math.floor(Math.random() * (high - low + 1) + low);
    } 
    
    alert (roltd(5,6)); // alerts 5 or 6
    </script>
    Last edited by Philip M; 07-13-2012 at 07:42 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.


  •  

    Posting Permissions

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