30

Possible Duplicate:
Generating random numbers in Javascript

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

Community
  • 1
  • 1
coure2011
  • 40,286
  • 83
  • 216
  • 349
  • 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. – paxdiablo Aug 29 '10 at 09:18
  • Not having a go at you, just pointing out another viewpoint you _might_ consider. – paxdiablo Aug 29 '10 at 09:19
  • @jigfox, one of the original goals for SO was to be _the_ place that Google sends you to for programming questions. So using Google searches as an alternative is not really apt. It's long been the case here that "Go to Google" answers get seriously downvoted. – paxdiablo Aug 29 '10 at 09:24
  • 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. – Darin Dimitrov Aug 29 '10 at 09:25
  • 1
    @paxdiablo, that might be, but If I have a programming question, I don't search SO, I search google, it's true that many results link to SO, but SO is not the only site with answers to programming questions, and a simple question like this is probably asked a hundred times before and also answered hundred times before, so why bother the people to answer it again, I prefer to not ask questions on SO, if I found the answer somewhere else, but SO is the first address, for asking my questions, if I don't find an answer with google, or bing or whatever you like. – jigfox Aug 29 '10 at 09:28
  • -1 for not searching the fine web. – Adam Matan Aug 29 '10 at 09:36
  • 2
    One of the goals of SO is that you end up here when you search for a question on google . Simple questions are just as valid. – nos Aug 29 '10 at 09:36
  • 3
    Seems to be a duplicate of http://stackoverflow.com/questions/1527803/generating-random-numbers-in-javascript – Antti Kissaniemi Aug 29 '10 at 09: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. – edeverett Aug 29 '10 at 09: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. – Hendrik Brummermann Aug 29 '10 at 09:59
  • 2
    @nhnb, I voted to close as *exact duplicate* as suggested by @Antti Sykäri 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. – Darin Dimitrov Aug 29 '10 at 10:06

4 Answers4

71
var min = -10;
var max = 10;
// and the formula is:
var random = Math.floor(Math.random() * (max - min + 1)) + min;
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 9
    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 Aug 29 '10 at 10:05
  • 5
    @edeverett, I voted to close as *exact duplicate* to http://stackoverflow.com/questions/1527803/generating-random-numbers-in-javascript 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. – Darin Dimitrov Aug 29 '10 at 10:10
  • https://gist.github.com/kerimdzhanov/7529623 – Dan K.K. Nov 18 '13 at 15:34
21

jQuery: $.randomBetween(minValue, maxValue);

Akyegane
  • 895
  • 6
  • 15
  • 6
    +1 for the jQuery answer –  Aug 29 '10 at 09:16
  • 32
    @RC: http://www.doxdesk.com/img/updates/20091116-so-large.gif – kennytm Aug 29 '10 at 09:17
  • @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 :-) – paxdiablo Aug 29 '10 at 09:22
  • @KennyTM, that's so hilarious. Thanks for posting it. I think this is a prime candidate for the daily WTF. – Darin Dimitrov Aug 29 '10 at 09:30
  • @RC, @pax, @Darin: (Quoting the reference: http://www.doxdesk.com/updates/2009.html#u20091116-jquery) – kennytm Aug 29 '10 at 09:32
  • 9
    -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... – Ivo Wetzel Aug 29 '10 at 09:38
  • 1
    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... – OXiGEN Sep 19 '20 at 05:02
  • On one hand you have one beautiful function with two parameters, and on the other hand you have an ugly complicated mismatch of variables and functions. Say what you like about jquery, but it's far more beautiful and elegant than pure javascript. – A Friend Sep 24 '20 at 05:31
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.

Adam Matan
  • 128,757
  • 147
  • 397
  • 562
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.

Narendra Jadhav
  • 10,052
  • 15
  • 33
  • 44