I am new to javascript and trying to understand Math.Random(). I have quite some experience with c# and I know that you can use random.next(1,10) to get numbers between 1 and 10. How is this with javascript. I tried serveral things like: math.Random(1,10) but this won't return the right value for me (between 1 and 10). I also informed alot of site's claiming this is the way to go. Like this one. However when I try this in w3schools I get not the suspected values. So please an explaination for this . Thank you in advance. Also I'm aware of this question but this do's not provide me the answer since there is no answer that answers the question: how to get a random number between two values. EDIT Okay so my question now is. Could someone please give me a brief explaination WHY I can't simply put two values and get a random value between them? Please stop marking this as a duplicate since this is not.
-
3The site you linked is the documentation for a math library. Compare `math.random` and `Math.random` – the difference in case *is* a significant difference. – Joey Jun 02 '15 at 08:23
-
`Math.random` Javascript is case sensitive language and Start using [MDN Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random) – Satpal Jun 02 '15 at 08:24
-
http://jsfiddle.net/a2e0a2oj/ – MortenMoulder Jun 02 '15 at 08:24
-
http://stackoverflow.com/questions/1527803/generating-random-numbers-in-javascript-in-a-specific-range – AmmarCSE Jun 02 '15 at 08:25
-
Your answer is all over the Internet, @Thealon. Google next time. – MortenMoulder Jun 02 '15 at 09:11
-
@Snorlax I did quite some research on this before I asked the question, however all I could find was not simular I'll delete this now to avoid future conflicts. – Thealon Jun 02 '15 at 09:15
-
@Thealon: "how to random between two numbers javascript" - Google that and it's the first result. – MortenMoulder Jun 02 '15 at 09:16
-
@Snorlax I got confused with this: http://mathjs.org/docs/reference/functions/random.html not knowing it was an external library. When I tried it in my javascript it didn't work, as I mention in my question I just started learning Js – Thealon Jun 02 '15 at 09:17
-
Surely you can find your answer through Google, though? math with a small m is the external library. Math is the Javascript one. You can do what you want without using an external library. – MortenMoulder Jun 02 '15 at 09:19
-
@Snorlax I wasen't aware of that. It can be confusing since all I currently know is c, c++ and c#. – Thealon Jun 02 '15 at 09:20
-
Google is your friend :) – MortenMoulder Jun 02 '15 at 09:21
-
@Snorlax Bing masterrace, jk haha. – Thealon Jun 02 '15 at 09:28
-
How come I flagged it and I still can delete this :(. – Thealon Jun 02 '15 at 09:29
3 Answers
JavaScript's Math.random()
(note the capitalization) returns a floating-point number between 0 (inclusive) and 1 (exclusive), e.g. 0 <= n < 1
.
To obtain a whole number within a given range, then, you do this:
Math.floor(Math.random() * (max - min)) + min
...which gives you a value between min
(inclusive) and max
(exclusive), e.g. min <= n < max
.
Okay so my question now is. Could someone please give me a brief explaination WHY I can't simply put two values and get a random value between them?
Because that's not how the JavaScript Math.random
function works. The specification (linked above) says how it works, and it doesn't take any arguments. Instead, with the value it returns, you can create your own function that works however you like.

- 1,031,962
- 187
- 1,923
- 1,875
-
This is half the answer I'm looking for, could you please provide more explaination why this is the way of getting one value between two given values and also explain more about why I can't simple give two arguments to Math.random. – Thealon Jun 02 '15 at 08:32
-
1@Thealon: Because that's not how the function is defined in the [specification](http://www.ecma-international.org/ecma-262/5.1/#sec-15.8.2.14). Functions that are not written by you work the way they are implemented, not how *you* want them to work. – Felix Kling Jun 02 '15 at 08:34
-
@Thealon— [*Math.random*](http://ecma-international.org/ecma-262/5.1/#sec-15.8.2.14) is specified as not taking any arguments, so you can supply as many as you like, they'll be ignored. – RobG Jun 02 '15 at 08:35
-
@RobG Could you please explain this: http://mathjs.org/docs/reference/functions/random.html – Thealon Jun 02 '15 at 08:37
-
-
-
@Thealon: That's not a part of the standard JavaScript library. It's an **add-on**. The standard JavaScript `Math.random` works as above. – T.J. Crowder Jun 02 '15 at 09:05
-
@Thealon: *"This is half the answer I'm looking for, could you please provide more explaination why this is the way of getting one value between two given values"* It's basic math. If you have a floating-point value between 0 and 1 and multiply it by, say, 10, you have a floating-point value between 0 and 10. If you want a whole number, `Math.floor` is one way to get one, and the appropriate way for what we're doing here. – T.J. Crowder Jun 02 '15 at 09:08
The documentation at Mozilla Developer Network is generally reliable, and they describe Math.random() here. Their description is:
The Math.random() function returns a floating-point, pseudo-random number in the range [0, 1) that is, from 0 (inclusive) up to but not including 1 (exclusive)
Your mention of using math.Random(1,10)
is based upon using an external library, Math.js, which has a different syntax.

- 7,311
- 3
- 26
- 50
-
I note the downvote; feel free to explain why this isn't "useful", considering it does actually answer the question. – Adrian Wragg Jun 02 '15 at 08:39
-
I didn't downvote you, also I know your feeling on getting downvotes without explaination. I liked your explaination but it didn't do it for me. Take my upvote for the effort and explaination. – Thealon Jun 02 '15 at 08:44
-
ok, Math.random() returns value between [0....1)
, so you need to multiply by 10 if you want to get 1...10]
in your case

- 1,808
- 21
- 27