1

I am trying to set a function that creates a random number between a range

I need to make it working with negative values so I can do

randomBetweenRange( 10,  20)
randomBetweenRange(-10,  10)
randomBetweenRange(-20, -10)

This is what I am trying, it is a bit confusing and at the moment randomBetweenRange(-20, -10) is not working..

function randomBetweenRange(a, b){
    var neg;
    var pos;

    if(a < 0){
        neg = Math.abs(a) + 1;
        pos = (b * 2) - 1;
    }else{
        neg = -Math.abs(a) + 1;
        var pos = b;
    }

    var includeZero = true;
    var result;

    do result = Math.ceil(Math.random() * (pos + neg)) - neg;
    while (includeZero === false && result === 0);

    return result;
}

How can I make it working?

neoDev
  • 2,879
  • 3
  • 33
  • 66
  • 1
    possible duplicate of http://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range – Himanshu Tanwar Oct 14 '16 at 13:00

4 Answers4

1

ASSUMING you will always have the little value on first, this code will do the tricks, see the comment below and don't hesitate to ask !

var a=parseInt(prompt("First value"));
var b=parseInt(prompt("Second value"));
var result = 0;

// Here, b - a will get the interval for any pos+neg value. 
result = Math.floor(Math.random() * (b - a)) + a;
/* First case is we got two neg value
 * We make the little one pos to get the intervale
 * Due to this, we use - a to set the start 
*/
if(a < 0) {
 if(b < 0) {
  a = Math.abs(a);
  result = Math.floor(Math.random() * (a + b)) - a;
 }
/* Second case is we got two neg value
 * We make the little one neg to get the intervale
 * Due to this, we use - a to set the start 
*/
} else {
 if(b > 0) {
  a = a*-1;
  result = Math.floor(Math.random() * (a + b)) - a;
 }
}
console.log("A : "+a+" | B : "+b+" | Int : "+(a+b)+"/"+Math.abs((a-b)));
console.log(result);
Aks
  • 395
  • 3
  • 11
1

If you want to generate a number between -50 and 50 - Get a random number between 0 and 100 then subtract 50

var randomNumber = Math.floor(Math.random() * 101) - 50;

console.log(randomNumber);
Weedoze
  • 13,683
  • 1
  • 33
  • 63
0
do result = Math.ceil(Math.random() * (pos + neg)) - neg;

Specifically Math.random() * (pos + neg) returns the wrong range. If pos = -20 and neg = -30, the range between pos and neg should be 10, but your operation returns -50. You should also add one to the range because its technically the amount of possibilities (ex: if you want to generate your function to return {0,1}, the range between pos and neg is 1, but there are two possibilities of numbers to return) and subtract another 1 from result because you're using Math.ceil

Your else clause also redeclares var pos

Kevin L
  • 1,066
  • 3
  • 12
  • 21
  • 1
    If it's not clear, this works by assuming the lowest number that can be generated is 0, and the highest is pos+neg. We then take neg away from the result and 0 / the lower base is now neg and the highest (pos+neg) becomes neg. I probably just made it less understandable. – Liam MacDonald Oct 14 '16 at 13:01
  • -1: You need to use `Math.floor` for a correct distribution, not `Math.ceil` – Bergi Oct 16 '16 at 22:11
0

You have declared the variable 'pos' in the beginning itself. Then why do you declare it in the 'else' part? ( var pos = b;)

Hence, for this statement, do result = Math.ceil(Math.random() * (pos + neg)) - neg;

'pos' will not have any value.

AnjuRaj
  • 298
  • 1
  • 10