In this article, we will have a look at `random()` method from `java.lang.Math` class, its usage with explanation and examples.

Math.random()
`java.lang.Math` class has a `random()` method which generates a `double` value greater than or equal to 0.0 and lesser than 1.0.
The value generated is random, which means that everytime this method is called, a new value will be returned.

`random()` is static, so there is no need to create an object of `Math` class and it can be called directly using class name.
Further, since `Math` class belongs to `java.lang` package, we need not import any explicit package.

Java docs for `Math.random()` state,

Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range.

Math.random() example
Below is an example program which invokes `Math.random()` multiple times and prints the value returned.

```public class RandomGenerator {
public static void main(String[] args) {
double value = Math.random();
System.out.println("First random value: " + value);
value = Math.random();
System.out.println("Second random value: " + value);
value = Math.random();
System.out.println("Third random value: " + value);
value = Math.random();
System.out.println("Fourth random value: " + value);
}
}```

This prints

First random value: 0.27976805895027346
Second random value: 0.690291590896412
Third random value: 0.2846875329581887
Fourth random value: 0.34820370070943185

All are random decimal values between 0.0 and 1.0.

Random values between range
`Math.random()` can be used to generate random numbers in a range.
1. Calculate the difference of lower and upper bound values.
2. Multiply this difference with the value returned by `Math.random()`.

Suppose you want to generate random decimal numbers between 5 and 15. The difference of these numbers is 10. This, when multiplied by `Math.random()` generates a value between 0 and 9(since `Math.random()` will return values >= 0.0 and  < 1.0).
In order to generate values till 10, add 1 to the result.
3. Finally, to generate numbers till upper bound, add the difference from Step 1 to the result. So, the generated number will be between 0 and 15.

Java program written as per this algorithm is given below.

```public class RandomNumberInRange {
public static void main(String[] args) {
int lower = 5;
int upper = 15;
double value = Math.random() * ((upper-lower) + 1) + lower;
System.out.println("First random value: " + value);
value = Math.random() * ((upper-lower) + 1) + lower;
System.out.println("Second random value: " + value);
value = Math.random() * ((upper-lower) + 1) + lower;
System.out.println("Third random value: " + value);
value = Math.random() * ((upper-lower) + 1) + lower;
System.out.println("Fourth random value: " + value);
}
}```

Output of this program is

First random value: 15.293224568526712
15.29Second random value: 6.3954644367753835
Third random value: 13.431685429240925
Fourth random value: 11.745938240905346

Random Integers
There are different methods to generate random integers with in a range and `Math.random()` is one of them.
The example in the previous section can be modified to generate integer values or whole numbers by adding a cast to the result as shown below.

```public class RandomIntegerGenerator {
public static void main(String[] args) {
int lower = 5;
int upper = 15;
int value = (int)(Math.random() * ((upper-lower) + 1)) + lower;
System.out.println("First random value: " + value);
value = (int)(Math.random() * ((upper-lower) + 1)) + lower;
System.out.println("Second random value: " + value);
value = (int)(Math.random() * ((upper-lower) + 1)) + lower;
System.out.println("Third random value: " + value);
value = (int)(Math.random() * ((upper-lower) + 1)) + lower;
System.out.println("Fourth random value: " + value);
}
}```

This prints

First random value: 6
Second random value: 11
Third random value: 5
Fourth random value: 5

All these are random integers between 5 and 15. Notice the use of parenthesis to maintain the order of evaluation.
Hope the article was useful. Cheers!!

0

Never Miss an article !

Get the new post delivered straight into your inbox, enter your email and hit the button

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

codippa will use the information you provide on this form to be in touch with you and to provide updates and marketing.