66% is suspiciously-close to two-thirds, so I'd check to see if your checking-of-heads logic is correct. Maybe you have some code that compares three (not two) values, and you're populating heads and tails, but not the third missing value. It's also good practice to not create a new Random object within a loop. Pretend new Random() creates a mysterious box that spits out random numbers. You don't need a thousand mysterious boxes - you just need to poke the box a thousand times. So call new Random() outside of the loop, but within the loop, call nextInt(2). Answer from x42bn6 on reddit.com
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Random.html
Random (Java Platform SE 8 )
2 days ago - Java™ Platform Standard Ed. 8 ... An instance of this class is used to generate a stream of pseudorandom numbers. The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald Knuth, The Art of Computer Programming, Volume 2, Section 3.2.1.) If two instances ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › generating-random-numbers-in-java
Generating Random Numbers in Java - GeeksforGeeks
April 24, 2025 - // Generating random number using java.util.Random; import java.util.Random; public class Geeks{ public static void main(String[] args) { // Creating the instance of Random class Random r= new Random(); // Generate random integers in range 0 to 999 int r1 = r.nextInt(1000); int r2 = r.nextInt(1000); // Printing random integers System.out.println("Random Integers: " + r1); System.out.println("Random Integers: " + r2); // Generate random doubles double rd1 = r.nextDouble(); double rd2 = r.nextDouble(); // Printing random doubles System.out.println("Random Doubles: " + rd1); System.out.println("Random Doubles: " + rd2); } }
🌐
Medium
medium.com › tuanhdotnet › generate-random-integers-in-java-using-math-random-and-related-concepts-e1dba0407aaa
Generate Random Integers in Java Using Math.random() and Related Concepts | by Anh Trần Tuấn | tuanhdotnet | Medium
September 25, 2025 - Math.random() relies on Java’s Random class under the hood. This means the method produces deterministic but seemingly random results based on a seed value.
🌐
TutorialsPoint
tutorialspoint.com › article › Generating-random-numbers-in-Java
Generating random numbers in Java
June 21, 2020 - import java.util.Random; import java.util.concurrent.ThreadLocalRandom; public class Tester { public static void main(String[] args) { generateUsingRandom(); generateUsingMathRandom(); generateUsingThreadLocalRandom(); } private static void generateUsingRandom() { Random random = new Random(); //print a random int within range of 0 to 10.
🌐
W3Schools
w3schools.com › java › java_howto_random_number.asp
Java How To Generate Random Numbers
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... You can use Math.random() method to generate a random number.
Find elsewhere
🌐
LeetCode
leetcode.com › problems › copy-list-with-random-pointer
Copy List with Random Pointer - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
🌐
iO Flood
ioflood.com › blog › java-random
Java Random: Generating Numbers with java.util.Random
March 5, 2024 - Math.random() is a simple, no-fuss way to get a random double, but requires additional steps to generate integers or other types of random values. Third-party libraries like Apache Commons Lang can provide additional utilities and flexibility, but require adding an external dependency to your project. While Java’s Random class and other methods offer powerful tools for generating random numbers, they’re not without their quirks.
🌐
Berkeley
titanium.cs.berkeley.edu › doc › java-api-1.0 › java › util › Random.html
java.util Class Random
java.lang.Object | +--java.util.Random · public class Random · extends Object · A Random class generates a stream of pseudo-random numbers. To create a new random number generator, use one of the following methods: new Random() new Random(long seed) The form new Random() initializes the ...
🌐
freeCodeCamp
freecodecamp.org › news › generate-random-numbers-java
Java Random Number Generator – How to Generate Integers With Math Random
November 25, 2020 - In this article, we will learn how to generate pseudo-random numbers using Math.random() in Java.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.util.random
Random Class (Java.Util) | Microsoft Learn
If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers. In order to guarantee this property, particular algorithms are specified for the class Random. Java implementations must use all the algorithms shown here for the class Random, for the sake of absolute portability of Java code.
🌐
Baeldung
baeldung.com › home › java › java numbers › random number generators in java
Random Number Generators in Java | Baeldung
January 8, 2024 - In this tutorial, we’ll compare the new RandomGenerator API with the old Random API. We’ll look at listing all available generator factories and selecting a generator based on its name or property. We’ll also explore the new API’s thread-safety and performance. First, let’s take a look at Java’s old API for random number generation based on the Random class.
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › util › random › package-summary.html
java.util.random (Java SE 21 & JDK 21)
January 20, 2026 - There are three groups of random number generator algorithm provided in Java: the Legacy group, the LXM group, and the Xoroshiro/Xoshiro group.
🌐
Java Mex
javamex.com › tutorials › random_numbers › java_util_random.shtml
Using java.util.Random
In our introduction to random numbers in Java, we saw the example of how to simulate a dice roll using the nextInt() method. The java.lang.Random class and its subclasses define a range of other methods for generating random numbers of different types and distributions.
Top answer
1 of 4
16

They're pseudorandom numbers, meaning that for general intents and purposes, they're random enough. However they are deterministic and entirely dependent on the seed. The following code will print out the same 10 numbers twice.

Random rnd = new Random(1234);
for(int i = 0;i < 10; i++)
    System.out.println(rnd.nextInt(100));

rnd = new Random(1234);
for(int i = 0;i < 10; i++)
    System.out.println(rnd.nextInt(100));

If you can choose the seed, you can precalculate the numbers first, then reset the generator with the same seed and you'll know in advance what numbers come out.

2 of 4
14

I want to know if there is a way to "predict" next generated number and how JVM determines what number to generate next?

Absolutely. The Random class is implemented as a linear congruential number generator (LCNG). The general formula for a linear congruential generator is:

new_state = (old_state * C1 + C2) modulo N

The precise algorithm used by Random is specified in the javadocs. If you know the current state of the generator1, the next state is completely predictable.

Will my code output numbers close to real random at any JVM and OS?

If you use Random, then No. Not for any JVM on any OS.

The sequence produced by an LCNG is definitely not random, and has statistical properties that are significantly different from a true random sequence. (The sequence will be strongly auto-correlated, and this will show up if you plot the results of successive calls to Random.nextInt().)

Is this a problem? Well it depends on what your application needs. If you need "random" numbers that are hard to predict (e.g. for an algorithm that is security related), then clearly no. And if the numbers are going to be used for a Monte Carlo simulation, then the inate auto-correlation of a LCNG can distort the simulation. But if you are just building a solitaire card game ... it maybe doesn't matter.


1 - To be clear, the state of a Random object consists of the values of its instance variables; see the source code. You can examine them using a debugger. At a pinch you could access them and even update them using Java reflection, but I would not advise doing that. The "previous" state is not recorded.

🌐
DigitalOcean
digitalocean.com › community › tutorials › random-number-generator-java
Random Number Generator in Java | DigitalOcean
August 4, 2022 - We can also use Math.random() to generate a double. This method internally uses Java Random class.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-random
Java Random | DigitalOcean
August 4, 2022 - If two Random instances have same seed value, then they will generate same sequence of random numbers. Java Random class is thread-safe, however in multithreaded environment it’s advised to use java.util.concurrent.ThreadLocalRandom class.
🌐
DevQA
devqa.io › java-random-number
Java Random Number Generation
In Java, we can generate random numbers by using the java.util.Random class.
🌐
Built In
builtin.com › articles › math.random-java
Java Math.random() Method Explained With Examples | Built In
June 23, 2025 - The Math.random method in Java generates a pseudorandom number between 0.0 and 1.0. Our expert explains how it works.