30

Possible Duplicate:
Generating random numbers in Javascript

How do I get a random number between −10 and 10 in JavaScript?

11
  • 5
    I'm guessing they've tried asking on the site meant as the premier site for asking programming-related question, despite the propensity for some of its members to ask inane questions before deigning to accept them into the brotherhood. Seriously, is it necessary to question everyone's motives? :-) Some people just need help and maybe haven't the knowledge or experience to try anything themselves first. We were all newbies once although I have to admit it was a long time ago for myself, lost in the dim dark past. Commented Aug 29, 2010 at 9:18
  • 2
    @paxdiablo, while I agree with you, I must admit that putting the body of the OPs question into the google search box will take less time than posting a question on SO and waiting for answers. IMHO developers must have the reflex to search for themselves first and when they encounter difficulties ask, that's why I find @Mitch's remark correct. Commented Aug 29, 2010 at 9:25
  • 3
    Seems to be a duplicate of stackoverflow.com/questions/1527803/… Commented Aug 29, 2010 at 9:37
  • 5
    How is this "not a real question" that needs closing? It seems clearly, if concisely, expressed and is a real, if basic, programming problem. It is eminently answerable. Commented Aug 29, 2010 at 9:50
  • 4
    @Mitch Wheat, @Mark Trapp, @kiamlaluno, @Antti Sykäri, @Darin Dimitrov. Please explain what is "ambiguous, vague, incomplete, or rhetorical and cannot be reasonably answered in its current form" about "How do I get a random number between −10 and 10 in JavaScript?". From my point of view the question is a very good example on how to write a in a very clear way. Commented Aug 29, 2010 at 9:59

4 Answers 4

71
var min = -10;
var max = 10;
// and the formula is:
var random = Math.floor(Math.random() * (max - min + 1)) + min;
Sign up to request clarification or add additional context in comments.

3 Comments

You closed the question for being: "ambiguous, vague, incomplete, or rhetorical and cannot be reasonably answered in its current form." Yet you answered it here. Can you you explain why?
@edeverett, I voted to close as exact duplicate to stackoverflow.com/questions/1527803/… and not as not a real question but unfortunately it was 3 vs 2 votes. If I was to vote to reopen this question it would be just to close it with the proper reason.
21

jQuery: $.randomBetween(minValue, maxValue);

8 Comments

+1 for the jQuery answer
@Kenny, I laughed so much I damn near cried. Surely that's a touch-up job, I can't imagine that voting pattern being real :-)
-1 For the jQuery overkill here. What's next adding .add() and .sub() to jQuery? >_> People should learn the JavaScript language, not the jQuery library...
Love the classic joke, but man, the outrage at a library that's already in many projects simplifying a task and making it more user friendly. Why in the world would a lib want to do that? Why would people want a quick and easy way to do things when there's the hard way? Excuse me while I go walk 5 km to do groceries instead of using my car...
|
9

Using not more than a simple Google search,

var randomnumber=Math.floor(Math.random()*21)-10

Math.random()*21)returns a number between 0 and 20. Substracting 10 would yield a random number between -10 and 10.

My advice: try to Google first, and than ask about the results, e.g.:

What is the best way to get a random number? I've googled and found methods X and Y, and wondered which is best for purpose Z.

1 Comment

function randbetween(min,max ) { return Math.floor(Math.random() * (max - min + 1)) + min; }
9

For example:

var min = -10;
var max = 10;

console.log(Math.floor(Math.random() * (max - min + 1) + min));
.as-console-wrapper { max-height: 100% !important; top: 0; }

See this jsrandom.php for some comparison of Math.floor, Math.ceil and Math.round.

1 Comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.