In Java, random numbers are generated using pseudo-random algorithms, meaning they follow a deterministic sequence but appear random. The two primary approaches are:

java.util.Random Class

This is the most commonly used method for generating random numbers in Java. It provides various methods to generate different types of random values.

  • Random random = new Random(); creates a new random number generator using the current time as the seed.

  • nextInt(int bound) returns a random integer from 0 (inclusive) to bound (exclusive).
    For example: random.nextInt(100) generates a number between 0 and 99.

  • nextInt() returns a random integer in the full range of int (including negative values).

  • nextDouble() returns a double between 0.0 (inclusive) and 1.0 (exclusive).

  • nextBoolean() returns true or false with equal probability.

To generate a random number between 1 and 100, use:

Random random = new Random();
int number = random.nextInt(100) + 1;

Math.random() Method

This is a static method that returns a double value between 0.0 (inclusive) and 1.0 (exclusive).

  • Math.random() is simpler to use but requires manual scaling and casting for integers.

  • To generate a number between 1 and 100:

    int number = (int)(Math.random() * 100) + 1;

ThreadLocalRandom (Java 7+)

For multi-threaded applications, ThreadLocalRandom is preferred as it avoids contention and is more efficient.

  • Use: ThreadLocalRandom.current().nextInt(1, 101);
    This generates a number from 1 (inclusive) to 101 (exclusive), i.e., 1 to 100.

Best Practice: Use java.util.Random for general use, ThreadLocalRandom in concurrent environments, and Math.random() only when simplicity is key and you're okay with casting and scaling.

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 )
1 week 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); } }
🌐
IronPDF
ironpdf.com › ironpdf for java › ironpdf for java blog › java help › math.random java
Math.random Java (How It Works For Developers)
June 21, 2025 - The Math.random() method is a static method that generates a pseudorandom double value greater than or equal to 0.0 and less than 1.0
🌐
Quora
quora.com › How-do-I-generate-a-random-number-in-Java-How-do-I-convert-it-into-an-integer-between-0-to-9
How to generate a random number in Java? How do I convert it into an integer between 0 to 9 - Quora
Answer: To get any random integer between 0 to 9 in Java there are two ways 1)You can use Math.random() method [code]class Demo { public static void main(String args[]) { int number = (int)(Math.random() * 10); System.out.println("Any random ...
🌐
University of Hawaiʻi at Mānoa
home.ifa.hawaii.edu › users › gmm › java › docs › java.util.Random.html
Class java.util.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 generator ...
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › Math › random
Java Math random() - Generate Random Number | Vultr Docs
September 27, 2024 - The random() method in Java's Math class is a fundamental tool for generating pseudo-random numbers between 0.0 (inclusive) and 1.0 (exclusive).
Find elsewhere
🌐
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.
🌐
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.
🌐
Mathbits
mathbits.com › JavaBitsNotebook › LibraryMethods › RandomGeneration.html
Java Random Generation - JavaBitsNotebook.com
Computers can be used to simulate the generation of random numbers. This random generation is referred to as a pseudo-random generation. These created values are not truly "random" because a mathematical formula is used to generate the values · The "random" numbers generated by the mathematical ...
🌐
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.
🌐
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.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › random
Math.random() - JavaScript | MDN
Note: Math.random() does not provide cryptographically secure random numbers. Do not use them for anything related to security.
🌐
Sentry
sentry.io › sentry answers › java › generating random numbers in java
Generating random numbers in Java | Sentry
September 15, 2024 - In such cases, it’s better to use the SecureRandom class to generate a secure random number. ... import java.security.SecureRandom; public class SecureDemo{ public static void main(String[] args) { SecureRandom secureRandom = new SecureRandom(); int n = secureRandom.nextInt(2); System.out.println(n); } }
🌐
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.
🌐
Speedrun.com
speedrun.com › mc
Minecraft: Java Edition - Speedrun.com
Minecraft: Java Edition(2011) Minecraft Series · PC · Category extensions Discord Website · Boost · 160 · Any% GlitchlessAny%All AdvancementsShow miscellaneous categories · Any% Glitchless Co-opAll Advancements Co-opAny% Glitchless (Demo)Any% Glitchless (Peaceful)Any% (Peaceful)Any% (Time Travel) Seed Type (Any% Glitchless) Random SeedSet Seed ·
🌐
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.