Take the 2-minute tour ×

How can I generate numbers between 7 to 10? So far all I've figured out is generating in a range from 0-10:

share|improve this question
asked soooo many times previously.... –  Mitch Wheat Jul 15 '11 at 2:43
And the difference between 7 (which you want) and 0 (which you can generate) is how much? –  S.Lott Jul 15 '11 at 2:50
The more comments of S.Lott's that I read, particularly the late-night ones, the more I like him. –  Jordan Jul 15 '11 at 3:19

4 Answers 4

up vote 14 down vote accepted

Math.floor(7 + Math.random() * 4) will generate numbers from 7 to 10 inclusive.

share|improve this answer
function getRandom(min, max) {
    return min + Math.floor(Math.random() * (max - min + 1));

for(var x = 0; x < 5; x++) {
    alert(getRandom(7, 10));
share|improve this answer
+1 for making it a general purpose function based on min, max. –  jfriend00 Jul 15 '11 at 3:01
multiplying random by (max-min) doesn't give you the range you want. Min needs to be a sort of "b" in the metaphorical mx+b here. –  NullVoxPopuli Dec 4 '13 at 18:40
@NullVoxPopuli, have you tried it? 43 satisfied customers so far, and I just checked the distribution range on it and it looks right to me. Here's a fiddle: jsfiddle.net/UCX9c/1 –  Jordan Dec 5 '13 at 4:53
oh hey, I didn't see the min at the beginning of that. lol. I thought you were just doing random() * max-min –  NullVoxPopuli Dec 5 '13 at 23:14
Ah, gotcha. Interesting to think of a random number within a range as the slope of a line. I tend to think of it as a set instead. Cool mental trick though. I'm sure a math person would laugh at me. –  Jordan Dec 6 '13 at 2:38

Just say this:

Math.floor(Math.random()*4) + 7

This will generate a random number from 0-3 and then add 7 to it, to get 7-10.

share|improve this answer
7 + Math.floor(Math.random()*4)
share|improve this answer

Your Answer


