lois v
lois v

Reputation: 21

Random Number in Lower and Upper Range

function randomRange(start, stop) {
  var low = Math.ceil(low);
  var high = Math.floor(high);
  return Math.floor(Math.random() * (high - low + 1)) + min;
}


function randomTester() {
  var start = parseInt(prompt("Enter lower part of range:"));
  var stop = parseInt(prompt("Enter upper part of range:"));
  alert("A random number in that range: " + randomRange(start, stop));
}


I am trying to display a random number in the upper and lower range of two random numbers that are both exclusive at both the min and max.

Upvotes: 0

Views: 1393

Answers (2)

Kat
Kat

Reputation: 7

What you have currently creates a range that is upper and lower inclusive. If you want make sure both are exclusive, you would have to do this:

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

Think about this logically. You start off with a lower range that is inclusive and an upper range that is exclusive. This code will establish a smaller range of the domain (high - low - 1) by subtracting 1 from the scale multiplier (high-low). This action will keep the lower range inclusive (since scale multiplier doesn't affect lower range because the range will always start at 0 until a shift happens) and change the higher range to even more exclusive than it was. This sets up an opportunity to shift the domain to the right by 1, which will cause the lower range to become exclusive (when shifted to the right, lower range cuts off the number 0, and therefore gains exclusivity), and the upper range to return from where it started, which is exclusive. Mathematically, this can be represented like this, supposing (high-low), or our scale multiplier, is 10:

0 <= x < 1
9(0 <= x < 1) //our scale multiplier was subtracted by 1
0 <= 9x < 9
(0 <= 9x < 9) + 1 //we now shift the entire domain by 1
1 <= 9x + 1 < 10 //which is exclusive on both ends, but we can simplify it
0 < 9x + 1 < 10 //which is exactly what we were looking for: the upper range (10) is excluded, as is the lower range (0).

Upvotes: 0

ibrahim mahrir
ibrahim mahrir

Reputation: 31712

The arguments for the function randomRange are start and stop but you are using low and high and min inside which are not defined yet.

function randomRange(start, stop) {
    var low= Math.ceil(low);
    //                ^^^^^
    var high= Math.floor(high);
    //                  ^^^^^^
    return Math.floor(Math.random() * (high - low + 1)) + min ;
    //                                                   ^^^^^
}  

Try this:

function randomRange(start, stop) {
    var low= Math.ceil(start);
    //                ^^^^^^^
    var high= Math.floor(stop);
    //                  ^^^^^^
    return Math.floor(Math.random() * (high - low + 1)) + low ; 
    //                                                   ^^^^^
}  

And why using those variables in the first place, just this will suffice I think:

function randomRange(start, stop) {
    return Math.floor(Math.random() * (stop - start)) + start; 
}  

Upvotes: 1

Related Questions