Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In Java, you can generate random numbers using the Math.random()
method or the Random
class from the java.util
package. In this tutorial, we will cover both methods for generating random numbers.
Math.random()
:The Math.random()
method generates a random double value between 0 (inclusive) and 1 (exclusive). You can scale and shift the result to obtain random numbers in the desired range.
Example: Generating a random integer between 1 and 10 (inclusive):
public class Main { public static void main(String[] args) { int randomInt = (int) (Math.random() * 10) + 1; System.out.println("Random integer between 1 and 10: " + randomInt); } }
Random
class:The Random
class provides more options for generating random numbers of various types, such as integers, longs, floats, and doubles.
Example: Generating a random integer between 1 and 10 (inclusive):
import java.util.Random; public class Main { public static void main(String[] args) { Random random = new Random(); int randomInt = random.nextInt(10) + 1; System.out.println("Random integer between 1 and 10: " + randomInt); } }
The Random
class also provides methods for generating random numbers with other types:
nextLong()
: Generates a random long value.nextFloat()
: Generates a random float value between 0 (inclusive) and 1 (exclusive).nextDouble()
: Generates a random double value between 0 (inclusive) and 1 (exclusive).Example: Generating a random float between 0 and 1:
import java.util.Random; public class Main { public static void main(String[] args) { Random random = new Random(); float randomFloat = random.nextFloat(); System.out.println("Random float between 0 and 1: " + randomFloat); } }
You can initialize the Random
class with a seed value, which determines the sequence of random numbers generated. This can be useful for testing or debugging purposes, as the same seed value will produce the same sequence of random numbers.
Example:
import java.util.Random; public class Main { public static void main(String[] args) { long seed = 123456789L; Random random = new Random(seed); for (int i = 0; i < 5; i++) { int randomInt = random.nextInt(10) + 1; System.out.println("Random integer between 1 and 10: " + randomInt); } } }
In this tutorial, we covered two methods for generating random numbers in Java: using the Math.random()
method and the Random
class. The Math.random()
method provides a simple way to generate random double values between 0 and 1, while the Random
class offers more flexibility and options for generating random numbers of various types and ranges.
Generating Random Integers in Java:
Math.random()
method.int randomInt = (int) (Math.random() * maxValue);
Java Math.random()
Method:
Math.random()
method returns a random double value between 0.0 (inclusive) and 1.0 (exclusive).double randomValue = Math.random();
Random Class in Java:
Random
class in Java provides more advanced random number generation capabilities.Random random = new Random(); int randomInt = random.nextInt(maxValue);
Seed and Randomness in Java:
Random seededRandom = new Random(123); // Seed value is 123 int randomInt = seededRandom.nextInt(maxValue);
Random Number Generation with java.util.Random
:
Random
class in java.util
provides more features and control over random number generation.java.util.Random random = new java.util.Random(); int randomInt = random.nextInt(maxValue);
Random Number within a Specific Range in Java:
min + random.nextInt(max - min + 1)
.int min = 5, max = 10; int randomInRange = min + random.nextInt(max - min + 1);
Generating Random Doubles and Floats in Java:
random.nextDouble()
or random.nextFloat()
.double randomDouble = random.nextDouble(); float randomFloat = random.nextFloat();
Secure Random Number Generation in Java:
SecureRandom
to ensure a high level of randomness.SecureRandom secureRandom = new SecureRandom(); int secureRandomInt = secureRandom.nextInt(maxValue);
Random Number Generation in Java Without Repetition:
List
and Collections.shuffle()
.List<Integer> numbers = new ArrayList<>(); for (int i = 1; i <= maxValue; i++) { numbers.add(i); } Collections.shuffle(numbers);
Thread-Safe Random Number Generation in Java:
ThreadLocalRandom
.int threadSafeRandom = ThreadLocalRandom.current().nextInt(min, max + 1);
Random Number Generation using Java 8 Streams:
List<Integer> randomNumbers = new Random().ints(5, 1, 100).boxed().collect(Collectors.toList());
Random Numbers and Probability Distributions in Java:
double value = new NormalDistribution().sample();
Random Number Seeding and Reseeding in Java:
Random random = new Random(); random.setSeed(System.currentTimeMillis()); // Reseeding with current time int reseededRandomInt = random.nextInt(maxValue);