# Random number generation in Java

**Random number generation **is an important and necessary subject. 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;
- and so on.

There are **several ways** to generate a random number. In this article we will cover the generation by means of **Math.random()**.

There is the package java.lang in the **Java class library**, which has the Math class, and the Math class in its turn has the random() method. Please see the picture below.

So, a random number is generated every time you call 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 for example 0,999 – i.e. any number less than 1. Try to run this code on your computer and you will see, that the console displays the 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) – you already know how to do it.

**2.And what if we need to generate a number from another interval, which is different from [0;1)?**

It’s easy! Let’s formally write our range as [a;b). That is, 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 the 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,

Lower limit: 0*3=0

Upper limit: 1+3=3

We get the range [0;3)

If we apply out formula, we get the following:

( Math.random() * (3 - 0) ) + 0 => Math.random() * 3

Which is displayed in the program.

**Example No.2**

Let us assume, 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,

Lower limit: 0+2=2

Upper limit: 1+2 =3

We get the interval [2;3)

Under 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,

Lower limit: 0*40=0

Upper limit: 1*40=40

We get the following interval: [0;40)

Step No.1: We add 20 to the interval [0;40). Accordingly,

Lower limit: 0+20=20

Upper limit: 40+20=60

We get the following interval: [20;60)

If we just apply it to our 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 the square bracket instead of round bracket. It means, that we want **the range to include the number 2**. 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,

Lower limit: 0*3=0

Upper limit: 1*3=3

We got the range [0;3). The bracket after the number 3 is round. That 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, it has only small differences.

In case if the real number and upper limit include b – [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. It means, that we want **the range to include the number 4**. 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,

Lower limit: 0*2=0

Upper limit: 1*2=2

We got 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 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,

Lower limit: 0+3=3

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); } } |