Exploring Random Numbers in Java

Pallavi Gaikwad
Posted on 25th Apr 2024 1:09 PM | 10 min Read | 60 min Implementation


##Randomnumbers ##java ##UtilPakages ##string ##packges ##random ##randomclass

This Blog will introduce you to Random Number In Java and follow it up with a programmatic demonstration.


Following pointers will be covered in this blog,

1.Java.util.Random

2.Math.random()

3.Java.util.concurrent.ThreadLocalRandom class



There are three methods to generate Random numbers in java using built-in methods and classes.

Java.util.Random class

Math. random method

ThreadLocalRandom class


Let's see ...


Generating Random Integers

Approach 1:

import java.util.Random;

publicclassApproach1 {
publicstaticvoidmain(String[] args) {
Randomrandom=newRandom();
// Generate a random integer between 0 (inclusive) and 100 (exclusive)
intrandomNumber= random.nextInt(100);
System.out.println("Random integer Number: " + randomNumber);

doublerand_doouble= random.nextDouble();
System.out.println("Random double Number: " + rand_doouble);
}
}


Output:


Note: By using random class will get the any random value between 0 to 99 after executing the code means same value may or may not generate for next execution.


Approach 2: Math. random method

The Math.random() method in Java is used to generate a random double value between 0.0 (inclusive) and 1.0 (exclusive). It returns a pseudo-random double value greater than or equal to 0.0 and less than 1.0.

package RandomNumber;

import java.util.Random;

publicclassApproach2 {
publicstaticvoidmain(String[] args) {
Randomrandom=newRandom();
System.out.println(Math.random());
}
}


Output:


Approach 3: Java.util.concurrent.ThreadLocalRandom class

A java.util.concurrent.ThreadLocalRandom is a utility class is useful when multiple threads .It improves performance and have less contention than Math.random() method.

package RandomNumber;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
publicclassApproach3 {

publicstaticvoidmain(String[] args) {
// Generate random integers within a range using ThreadLocalRandom
// Generates random integer between 1 (inclusive) and 101 (exclusive)
intrandomInt1= ThreadLocalRandom.current().nextInt(1, 101);
System.out.println("Random Integer 1: " + randomInt1);

// Generate random doubles within a range using ThreadLocalRandom
// Generates random double between 0.0 (inclusive) and 1.0 (exclusive)
doublerandomDouble1= ThreadLocalRandom.current().nextDouble(0.0, 1.0);
System.out.println("Random Double 1: " + randomDouble1);

// Generate random integers within a range with a specified origin
// Generates random integer between 10 (inclusive) and 21 (exclusive)
intrandomInt2= ThreadLocalRandom.current().nextInt(10, 21);
System.out.println("Random Integer 2: " + randomInt2);

}
}


Output:


GitRepository:https://github.com/PallaviGajananGaikwad/RandomNumbers.git


Conclusion:

Random number generation is a fundamental aspect of many Java applications. By leveraging the functionalities provided by java.util.Random, developers can introduce controlled randomness into their programs, enabling a wide range of functionalities from game development to statistical analysis.


All Comments ()
Do You want to add Comment in this Blog? Please Login ?