neoDev
neoDev

Reputation: 3039

Generate random number between range including negative in javascript

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?

Upvotes: 2

Views: 3331

Answers (4)

Aks
Aks

Reputation: 395

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);

Upvotes: 1

Weedoze
Weedoze

Reputation: 13953

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);

Upvotes: 2

Kevin L
Kevin L

Reputation: 1066

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

Upvotes: 0

AnjuRaj
AnjuRaj

Reputation: 298

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.

Upvotes: 0

Related Questions