The Math.random()
method in Java returns a random double
value in the range $ [0.0 - 1.0) $. That’s read as 0.0 inclusive to 1.0 exclusive, which means that it can be 0.0, but will never quite reach 1.0. Think of it as a range from $ 0.0 - 0.\overline{999} $ with as many 9s as Java can store.
Let’s look at a few examples of how this works.
Method Call | Range |
---|---|
Math.random() |
$ [0.0 - 1.0) $ |
Math.random() * 10 |
$ [0.0 - 10.0) $ |
Math.random() * 10 + 1 |
$ [1.0 - 11.0) $ |
Math.random() * 7 - 1 |
$ [-1.0 - 6.0) $ |
In the second row above we’re multiplying the result of Math.random()
by 10. That means that the range is now $ [0.0 - 10.0) $. The number we multiply gives us the upper bound, although we’ll never quite get the upper bound because Math.random()
never returns 1.0
.
In the third and fourth rows we’re shifting the range of possible values. In the third row we’re adding 1 to the result of Math.random() * 10
. That means that the range is now $ [1.0 - 11.0) $. In the fourth row we’re subtracting 1 from the result of Math.random() * 7
. That means that the range is now $ [-1.0 - 6.0) $.
And a quick hint… The shift value, the number we’re adding or subtracting, is always the smallest possible value in the range. If there isn’t a shift value, the range starts at 0.0.
Let’s look at a quick real world example. Say that you’re flipping a coin and you want it to be heads 50% of the time and tails 50% of the time. You might write code something like this.
if (Math.random() < 0.5) {
System.out.println("Heads");
} else {
System.out.println("Tails");
}
Given enough runs of this code, you should see that the output is roughly 50% heads and 50% tails.
The Math.random()
method returns a double
value, but what if you want an integer? You can use the Math.random()
method to generate a random double
value and then cast it to an int
value. For example, if you want a random integer between 1 and 10, you could write code like this.
int randomInt = (int) (Math.random() * 10 + 1);
This code will generate a random double
value between 1.0 inclusive and 11.0 exclusive, and then cast it to an int
value. The result will be an integer between 1 and 10, inclusive on both ends
Time for a few more examples.
Method Call | Range |
---|---|
(int) (Math.random() * 10 + 1) |
$ [1 - 11) $ or $ [1 - 10] $ |
(int) (Math.random() * 7) - 1 |
$ [-1 - 7) $ or $ [-1 - 6] $ |
(int) (Math.random() * 6) |
$ [0 - 6) $ or $ [0 - 5] $ |
Really important thing to notice because it’s a really common mistake is that Math.random() * numberRange
needs to be wrapped in parentheses before it’s cast. Many students will write (int)Math.random * 10
, which is always 0 because Math.random()
is getting cast to an int
first and then multiplied. The parentheses are needed to make sure that the multiplication happens first.
Let’s look at a standard 6 sided die for an example this time. When you roll a die the number you get is between 1 and 6, inclusive on both ends. The code would look something like this.
int dieRoll = (int) (Math.random() * 6 + 1);
But, what if we’re rolling 2 dice? This time the lower bound is 2 and the upper bound is 12. The code would look something like this.
int dieRoll = (int) (Math.random() * 11 + 2);
11 total possible values, plus a shift of 2 so that the range is from 2 to 12.
The general formula for generating a random integer between lowerBound
and upperBound
is
int rnd = (int) (Math.random() * (upperBound - lowerBound + 1) + lowerBound);
You can also think of it like this, which I find easier to use when you’re working backwards.
int numberOfPossibleValues = /* An integer value */;
int shift = /* An integer value */;
int rnd = (int)(Math.random() * numberOfPossibleValues + shift);
And it doesn’t matter where the shift value is. The following lines are all equivalent.
int rnd1 = (int)(Math.random() * numberOfPossibleValues + shift);
int rnd2 = (int)(Math.random() * numberOfPossibleValues) + shift;
int rnd3 = shift + (int)(Math.random() * numberOfPossibleValues);
```
Next you’ll find a randomly generated practice problem for you to practice working with Math.random()
. For each you’ll see a description of the problem, a code snippet, and a field to enter your answer. There is also a button to check your answer and another to generate a new problem. When you click the Check Answer button it will tell you whether your answer is correct and also give you a short explanation of how to find the correct answer.
The problems are randomly generated every time you click New Question, so there’s plenty of practice for you to do to bump up your confidence.