Random Number Generation in Java
This is one of the articles from our Java Tutorial for Beginners.
Random number generation is an important and necessary topic. Indeed, it will come in handy for you on countless occasions.
For example:
- when completing a group of random numbers
- when shuffling a pack of cards
- when selecting a random element of a sequence
- etc.
There are several ways to generate a random number. In this article, we will cover generation by means of Math.random().
In the Java class library, there is a package called "java.lang" and inside of it is the Math class. And inside the Math class is the random() method. Please see the picture below.
So, a random number is generated every time you summon Math.random() by means of the special algorithm (on the particular instruction). Is it possible to predict which number will be generated? In theory, it is possible, but it’s really hard to do. And since there is a small chance to predict which number will be generated, such numbers are called pseudorandom, instead of just random.
You need to know three things:
1. By default, Math.random() generates real numbers from the interval [0;1), i.e. from 0 inclusive up to 1 exclusive.
“Up to 1 exclusive” means that Math.random cannot generate the number 1. But it can generate 0.999 – i.e. any number less than 1. Run this code on your computer and you will see that the console displays real numbers from 0 to any number less than 1.
1 2 3 4 5 6 7 |
public class Test { public static void main(String[] args){ double a = Math.random(); System.out.println (a); } } |
So if you need to generate a random number from the interval [0;1), then you already know how to do it.
2. And what if we need to generate a number from a range which is not [0;1)?
It’s easy! Let’s formally write our range as [a;b) i.e. the lower limit is a, and the upper limit is b. Then, in order to generate a real number within the preset range we need to write the following:
( Math.random() * (b-a) ) + a
Let’s look at the examples below.
Example No. 1
So, in order to get a random real number within the range [0;3) (3 exclusively), we use the following:
1 2 3 4 5 6 7 |
public class Test { public static void main(String[] args){ double a = Math.random()*3; System.out.println(a); } } |
How does it work?
The range [0;1) is multiplied by 3. Accordingly,
The lower limit: 0*3=0
The upper limit: 1+3=3
We get the range: [0;3)
If we apply the formula, we get the following:
( Math.random() * (3 - 0) ) + 0 => Math.random() * 3
And this is what is displayed in the program.
Example No. 2
Let us suppose that we need to get a floating point number within the range [2;3) (3 exclusive).
1 2 3 4 5 6 7 |
public class Test { public static void main(String[] args){ double a = Math.random()+2; System.out.println(a); } } |
How does it work?
We add 2 to the interval [0;1). Accordingly,
The lower limit: 0+2=2
The upper limit: 1+2 =3
We get the interval: [2;3)
If we apply the formula, we get the following:
( Math.random() * (3 - 2) ) + 2 => Math.random() * 1 + 2 => Math.random() + 2
Example No. 3
Let’s say we need to get a real number in the range [20;60) (60 exclusive).
1 2 3 4 5 6 7 |
public class Test { public static void main(String[] args){ double a = 20 + Math.random()*40; System.out.println(a); } } |
How does it work?
Step No. 1: The interval [0;1) is multiplied by 40. Accordingly,
The lower limit: 0*40=0
The upper limit: 1*40=40
We get the following interval: [0;40)
Step No. 2: We add 20 to the interval [0;40). Accordingly,
The lower limit: 0+20=20
The upper limit: 40+20=60
We get the following interval: [20;60)
If we apply the formula, we get:
( Math.random() * (60 - 20) ) + 20 => Math.random() * 40 + 20
Example No. 4
We need a real number in the range [ -100; +100) (100 exclusive).
1 2 3 4 5 6 7 |
public class Test { public static void main(String[] args){ double a = Math.random()*200 - 100; System.out.println(a); } } |
According to the formula:
( Math.random() * (100 - (-100)) ) + (-100) => Math.random() * 200 - 100
3. And how do I generate an integer value?
Example No. 5
Let’s imagine that we need to generate a number in the range [0;2]. Please note that after the number 2, we used a square bracket instead of a round bracket. This means that we want the range to include the number 2. So how do we do it? Like this:
1 2 3 4 5 |
public class Test { public static void main(String[] args){ int a = (int) ( Math.random() * 3 ); } } |
How does it work?
Step No. 1: The interval [0;1) is multiplied by 3. Accordingly,
The lower limit: 0*3=0
The upper limit: 1*3=3
We got the range [0;3). The bracket after the number 3 is round. This means that the number 3 is not included in the range, i.e. 2.999 is the maximum number which can be generated in this range. In short, we are dealing with real (fraction) numbers.
Step No. 2: That's why we apply (int) before Math.random()*3. The fractional part is cut off and we get the range [0;2].
If we check our formula, we see it hasn't changed much.
If it's a real number and the upper limit includes b, then [a;b] :
( Math.random() * (b - a + 1) + a
And if it includes only the integer numbers:
(int)(( Math.random() * (b - a + 1) + a)
Example No. 6
We need to generate a number within the interval [3;4]. Please note that we used the square bracket after the number 4. This means that we want the range to include the number 4. So how do we do it? Like this:
1 2 3 4 5 |
public class Test { public static void main(String[] args){ int a = 3 + (int) ( Math.random() * 2 ); } } |
How does it work?
Step No. 1: We multiply the range [0;1) by 2. Accordingly,
The lower limit: 0*2=0
The upper limit: 1*2=2
We get the range [0;2). The bracket after the number 2 is round. That means that the number 2 is not included in the range, i.e. 1,999 is the maximum number which can be generated in this range. And it also means that we are dealing with a real (fraction) number.
Step No.2: That's why we apply (int) before Math.random()*3. The fractional part is cut off and we get the range [0;1]. Please note: now the range includes the number 1.
Step No.3: We add 3 to the interval [0;1]. Accordingly,
The lower limit: 0+3=3
The upper limit: 1+3=4
We got the new range [3;4].
Now we apply the formula:
(int)(Math.random() * (4 - 3 + 1) + 3) => (int)(Math.random() *2 + 3)
Example No. 7
We need an integer value within the range [-100; +100] (100 inclusive).
1 2 3 4 5 6 7 |
public class Test { public static void main(String[] args){ int a = (int) (Math.random()*(200+1)) - 100; System.out.println(a); } } |
Example No. 8
We need an integer value within the range [-200; 400] (400 inclusive).
1 2 3 4 5 6 7 |
public class Test { public static void main(String[] args){ int a = (int) (Math.random()*(600+1)) - 200; System.out.println(a); } } |
Example No. 9
We need an integer value within the range [1;2] (2 inclusive).
1 2 3 4 5 6 7 |
public class Test { public static void main(String[] args){ int a = (int) (Math.random()*(1+1)) + 1; System.out.println(a); } } |
You can find more articles in our Java Tutorial for Beginners.