0

I need to generate a random number that's between -40 and -10 and 10 and 40. So the results could be 25, but never -5 or 5.

I've looked online, but the solutions seem overly complex. Is there any way to do it in just one or two lines?

1
  • 1
    What solutions are overly complex? Commented May 24, 2013 at 19:38

7 Answers 7

3

Math.random() will return a random number between 0 and 1.

Create your own function to create a random number between your min and max numbers. Scale a random number between your min and max, then offset by you min value.

function randomBetween(min, max) {
  return Math.random() * (max - min) + min;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Doesn't answer the question asked.
It doesn't, but I came here from a Google search looking for this exact piece of code though. I can now continue writing code, instead of Googling :-)
1

Generate a number between 10 and 40 and then generate a 1 or 0, if 0 convert it to negative 1 and multiply by either result Math.random() * 30 + (Math.random() < 0.5 ? -40 : 10);

8 Comments

What's the most elegant way to write this?
var result = Math.random() * 30 + (Math.Random() < 0.5 ? -40 : 10);
@MattHouser, nice simplification
That'll work too, but requires calling random() twice, which will be twice as slow as necessary. Also, he seems to want integer values.
@LeeDanielCrocker, 2 calls to random is not a huge speed hit this obviously isn't a performance critical application and my solution is more elegant and readable than yours not to mention more extendable to other cases
|
0

That's 62 distinct values, 31 in each subrange, so:

r = Math.floor(Math.random()*62);
if (r < 31) { r -= 40; } else { r -= 21; }

Comments

0
var n = Math.random()*62;
return n - (n < 31 ? 40 : 21);

4 Comments

Close, but no cigar. Change those constants to 62, 31, 40, and 21. OP specified inclusive ranges.
Thought I might have been susceptible to an off by 1 error. I really shouldn't attempt writing code this late.
That's better. OP seems to imply integers, though. Not sure if that's really what he wants or not.
Right..well we can floor that.
0
for(int i=15;i<=46;i++)
{
    double random=Math.rand()*5+i; 
    int rand=(int)random;
    System.out.println(rand);

}

The above code generates the random number between 15-50

Math.random()*5=i  

This gives the lower value i.e 15 here in this case

Note: you need to subtract 4 from the upper bond of the value i.e here if u use i<=50 this will be printing no up to 54 , similarly if you want to generate no. b/w 50-80 use (int i=50;i<=76;i++)

hope this helped :)

Comments

0

Short Answer:
It could be tricky but definitely the best way is to write your own function.

var random = (Math.random() - 0.5) * 2;

if (random >= 0){
    random = lerp(10, 40, random);
} else {
    random = lerp(-40, -10, Math.abs(random));
}

function lerp (min, max, value){
    return (1 - value) * min + value * max;
}    

Explanation:

There are (3) three steps you need to follow in order to achieve what you need.

  1. A random number (between -1 and 1).

    let random = (Math.random() - 0.5) * 2
    
  2. An if condition to know if the value is negative or positive.

    if (random >= 0){ /* Is Positive */ }
    else { /* Is Negative */ }
    
  3. Lerp between the values you need (-40 to-10 if negative or 10 to 40 if positive).

    // This function interpolates between two numbers based on a normalized value (between 0 and 1)
    function lerp (min, max, value){
       return (1 - value) * min + value * max;
    }
    
    

Comments

-1

The algorithm is (rand*(max-min))+min

Math.floor(Math.random()*30)+10

and

Math.floor(Math.random()*-30)-10

1 Comment

He wants to generate a single number given the conditions.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.