This example shows how to generate random numbers using the random method of the Java Math class including generating random numbers between a specific range.

## How to generate random numbers in Java?

Use the `random`

static method of the Math class to generate random numbers in Java.

1 | public static double random() |

This method returns a double number greater than or equal to 0.0 and less than 1.0 (Note that the 0.0 is inclusive while 1.0 is exclusive so that 0 <= n < 1)

### a) How to generate a random number between 0 and 1?

1 2 3 4 5 6 7 8 9 10 11 12 13 | package com.javacodeexamples.mathexamples; public class GenerateRandomNumberExample { public static void main(String[] args){ System.out.println("Random numbers between 0 and 1"); for( int i=0; i < 10; i++ ){ System.out.println( Math.random() ); } } } |

Output (could be different for you as these are random numbers)

1 2 3 4 5 6 7 8 9 10 11 | Random numbers between 0 and 1 0.12921328590853476 0.7936354242494305 0.08878870565069197 0.12470497778455492 0.1738593303254422 0.6793228890529989 0.5948655601179271 0.9910316469070309 0.1867838198026388 0.6630122474512686 |

### b) Between 0 and 100

It is fairly easy task to generate random numbers between 0 and 100. Since `random()`

method returns a number between 0.0 and 1.0, multiplying it with 100 and casting the result to an integer will give us a random number between 0 and 100 (where 0 is inclusive while 100 is exclusive).

1 2 | int randomNumber = (int) (Math.random() * 100); System.out.println("Random Number: " + randomNumber); |

### c) Between a specific range

Since the `random`

method returns a double value between 0.0 and 1.0, we need to derive a formula so that we can generate numbers in the specific range.

Let’s do that step by step. Suppose you want to generate random numbers between 10 and 20. So the minimum number it should generate is 10 and the maximum number should be 20.

**Step 1:**

First of all, we need to multiply the `random`

method result with the maximum number so that it returns value between 0 to max value (in this case 20) like given below.

1 | int randomNumber = (int) ( Math.random() * 20 ); |

The above statement will return us a random number between 0.0 and 19. That is because multiplying 0.0 – 0.99 with 20 and casting the result back to int will give us range of the 0 to 19.

#### Step 2:

Step 1 gives us a random number between 0 and 19. But we want a random number starting from 10, not 0. Let’s add that number to the result.

1 | int randomNumber = 10 + (int) ( Math.random() * 20 ); |

#### Step 3:

Now the number starts from 10 but it goes up to 30. That is because adding 10 to 0-19 will give us 10-29. So let’s subtract 10 from 20 before multiplication operation.

1 | int randomNumber = 10 + (int) ( Math.random() * (20 - 10) ); |

#### Step 4:

The random number generated by the above formula gives us a range between 10 and 19 (both inclusive). The number range we wanted was between 10 and 20 (both inclusive). So let’s add 1 to the equation.

1 | int randomNumber = 10 + (int) ( Math.random() * ((20 - 10) + 1) ); |

A final result is a random number in the range of 10 to 20.

## The general formula to generate random numbers in a specific range

Formula to generate random number in the range of min and max is as given below.

1 | Random Number = Min + (int) ( Math.random() * ((max – min) + 1) ) |

The final example to generate random numbers in a specific range is given below.

1 2 3 4 5 6 | int min = 10; int max = 20; int randomNumber = min + (int) ( Math.random() * ((max - min) + 1) ); System.out.println("Random number in the range of min & max is: " + randomNumber); |

This example is a part of the Java Math class tutorial with examples.

Please let me know your views in the comments section below.

## Add Comment