Your formula generates numbers between min and min + max.

The one Google found generates numbers between min and max.

Google wins!

Answer from pjz on Stack Overflow
🌐
Tutorialspoint
tutorialspoint.com › java › lang › math_random.htm
Java - Math random() method
The Java Math random() 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.
🌐
Career Karma
careerkarma.com › blog › java › how to use java math.random
How to Use Java Math.random: A Step-By-Step Guide | Career Karma
December 1, 2023 - The Math.random()Java method generates a pseudorandom number between 0.0 and 1.0. The resulting random number can be multiplied to get a range outside 0-1, and the result can be 0 but is always less than 1. When you’re programming, there are ...
Discussions

Random numbers with Math.random() in Java - Stack Overflow
Sign up to request clarification or add additional context in comments. ... For the sake of completeness, when you say 'complete', do you mean including min and excluding max? 2014-01-29T11:00:28.897Z+00:00 ... Your formula generates numbers between min and min + max. Random random = new Random(1234567); int min = 5; int max = 20; while (true) { int x = (int)(Math... More on stackoverflow.com
🌐 stackoverflow.com
How does Math.random() EXACTLY work in Java? - Stack Overflow
Find centralized, trusted content ... you use most. Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... class in Java. (And) I was wondering about the class. For example, I'm trying to write a dice game that needs random numbers from 1 to 6 each "roll". This is where math.random( ) ... More on stackoverflow.com
🌐 stackoverflow.com
[Java] How does Math.random() generate a random number?
See computational methods under: https://en.wikipedia.org/wiki/Random_number_generation Pseudo-random typically means there's an algorithm for generating the numbers that have certain probability distributions. This algorithm usually has some initial "seed" number. Given a seed, it produces the same sequence of random numbers (hence pseudo-random). For some applications, pseudo-random is good enough (esp. since you can repeatedly test the same values). More on reddit.com
🌐 r/learnprogramming
4
6
December 14, 2017
Do you guys use Math.random() or the Random class more often for generating random numbers?
I always use Random. The reason is that if a class depends on random number generation, it actually has a dependency on another thing whose job it is to generate random numbers (you could even make the argument this is a hidden I/O operation). It's damn near impossible to write a unit test against a class or method that calls a global function because you have no access to mock out the response for that function within a test. This is even more the case for classes that rely on randomly generated numbers. Passing a Random into a class that needs access to random numbers makes it easy to unit-test that class, as well as substitute in alternative RNG as needed, for example using SecureRandom which implements the Random interface, providing a more even distribution of generated numbers at the cost of speed. Random number generators, at least in Java, tend to mutate state internally - which is dangerous for multi-threaded programming. You want to minimize the amount of state mutation in a program to enable multi-threading, and Random turns the mutable state for random number generation to be scoped to an object rather than to the entire global space as Math.random() does. Other benefits of using Random over Math.random(): You can pass a single instance of Random into the first class that needs the ability, and pass the same instance around into lower classes as they require it as well. This means that, on your program runs, if you output the seed number somewhere like into a log or temporary file, you can seed your program with the same seed value and generate exactly the same behavior, deterministically. This makes debugging issues that arise in your program much easier. The Random interface is superior, allowing you to request random booleans, byte sequences, doubles, floats, longs, and ints. It also allows you to request a random int within a specific range. All of these let you avoid inserting the same boilerplate math that uses a Math.random(). More on reddit.com
🌐 r/java
18
6
October 10, 2012
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-math-random-method-examples
Java Math random() Method - GeeksforGeeks
December 20, 2025 - To generate random integers in a range: (int)(Math.random() * (max - min + 1)) + min · Note: Math.random() is not suitable for cryptographic purposes. For better randomness and control, use java.util.Random or SecureRandom. The method always returns values less than 1.0, never equal to 1.0.
🌐
Educative
educative.io › answers › how-to-use-the-mathrandom-method-in-java
How to use the Math.random() method in Java
import java.lang.Math; //importing Math class in Java · class MyClass { public static void main(String args[]) { double rand = Math.random(); // generating random number · System.out.println("Random Number: " + rand); // Output is different ...
🌐
Programiz
programiz.com › java-programming › library › math › random
Java Math.random()
class Main { public static void main(String[] args) { int upperBound = 20; int lowerBound = 10; // upperBound 20 will also be included int range = (upperBound - lowerBound) + 1; System.out.println("Random Numbers between 10 and 20:"); for (int i = 0; i < 10; i ++) { // generate random number // (int) convert double value to int // Math.random() generate value between 0.0 and 1.0
🌐
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 - This example simulates rolling ... to use Math.random() to generate numbers within a specific range by multiplying the result by the maximum value and adding one to shift the range from 0-5 to 1-6. import java.util.Random; ...
Find elsewhere
🌐
W3Schools
w3schools.com › java › ref_math_random.asp
Java Math random() Method
The random() method returns a random number between 0 and 1. This method never returns exactly 1, but it can return 0. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com
🌐
freeCodeCamp
freecodecamp.org › news › generate-random-numbers-java
Java Random Number Generator – How to Generate Integers With Math Random
November 25, 2020 - If we only want double type pseudo-random numbers in our application, then we can use Math.random() .
🌐
CodeAhoy
codeahoy.com › java › Math-Random-method-JI_17
Java Math.random() Method with Examples | CodeAhoy
October 12, 2019 - Note that just like all other methods of the Math class, Math.random() is a static method so you can call it directly on the Math class without needing an object. It returns a value of type double. Another thing to keep in mind is that this method creates a new object of type Random which it ...
🌐
CodeGym
codegym.cc › java blog › java math › java math random() method
Java Math random() Method
December 5, 2024 - Let’s look at an example of using ... 1 to 50. public class RandomNumberInRange { public static int getRandom(int min, int max) { int range = (max - min) + 1; int random = (int) ((range * Math.random()) + min); return random; } public static ...
Top answer
1 of 3
9

I was however, puzzled why a random number (say 0.78396954122) multiplied by 6 could become a random number between 1 to 6. Supposedly the random number 0.78396954122 multiplied by 6, is always 6, right?

No. 0.78396954122 * 6 is 4.70381724732 which when truncated by (int) becomes 4 which then becomes 5 when the + 1 is done.

Similarly, Math.random might return 0.2481654 which when multiplied by 6 is 1.4889924, which (int) truncates to 1, and then becomes 2 thanks to the + 1.

If it were, say, 0.0485847 * 6, that would be 0.2915082, which (int) truncates to 0, which becomes 1 thanks to the + 1.

The lowest value Math.random will ever return is 0.0, which is truncated to 0 by (int). 0 * 6 is 0, which becomes 1 after the + 1. The highest value Math.random will ever return is 0.99999999(etc) (e.g.,. it will be < 1). Multiplying that by 6 gives us 5.999999(etc) which is truncated to 5 by (int), and then turned into 6 by + 1.

So that's how we get the range 1-6 from that code.

2 of 3
3

Math.random()

It generates a random number between 0.0 to 1.0(always less then 1.0).So,in your problem as you said that you want to generate random number between 1 to 6 that is why you have multiplied it by 6, so that any random number the function will return will be multiplied by 6 and then + 1 ,so that it always stay between 1-6 .

And like you said , 0.78396954122 * 6 is 6 then you are wrong in calculation here,

0.78396954122 * 6 is 4.7 which will be rounded off to 4 by the (int). This is called casting in java,and at last it will become 4+1=5

That is why you get a number between 1-6 by this

🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › Math › random
Java Math random() - Generate Random Number | Vultr Docs
September 27, 2024 - ... double randomNumber = ... it is executed. The value lies between 0.0 and 1.0. Use Math.random() in conjunction with type casting to generate a random integer....
🌐
GeeksforGeeks
geeksforgeeks.org › java › generating-random-numbers-in-java
Generating Random Numbers in Java - GeeksforGeeks
April 24, 2025 - ... // Java program to demonstrate ... { // Generating random doubles System.out.println("Random doubles: " + Math.random()); System.out.println("Random doubles: " + Math.random()); } }...
🌐
Medium
medium.com › @AlexanderObregon › javas-math-random-method-explained-bafa91bf49a6
Java’s Math.random() Method Explained | Medium
November 30, 2024 - The Math.random() method in Java is a commonly used feature for generating pseudo-random numbers. This article explains how the method…
🌐
iO Flood
ioflood.com › blog › math-random-java
Java Math.random() Function: Number Generation Guide
February 29, 2024 - Math.random() is a built-in function in Java, used to generate pseudo-random numbers. The function returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
🌐
Reddit
reddit.com › r/learnprogramming › [java] how does math.random() generate a random number?
r/learnprogramming on Reddit: [Java] How does Math.random() generate a random number?
December 14, 2017 -

I've looked up the source code and this was the purpose statement:

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. When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expression new java.util.Random() This new pseudorandom-number generator is used thereafter for all calls to this method and is used nowhere else. This method is properly synchronized to allow correct use by more than one thread. However, if many threads need to generate pseudorandom numbers at a great rate, it may reduce contention for each thread to have its own pseudorandom-number generator.

What does this mean? How does it return a different double each time? I've always been incredibly curious as to how random number generators worked.

🌐
Scaler
scaler.com › home › topics › java math random() method
Java Math random() Method- Scaler Topics
February 14, 2024 - The Math.random() in java helps us to generate random double values between 0 and 1.
🌐
Medium
medium.com › @generativeai.saif › java-random-number-generator-5-methods-explained-with-examples-bf9e53abed3c
Java Random Number Generator: 5 Methods Explained with Examples | by Saif Ali | Medium
April 6, 2025 - Generating random numbers within a specific range ∘ Using java.util.Random: ∘ Using Math.random(): ∘ Using ThreadLocalRandom: · Choosing the right random number generator · Conclusion · Random numbers in computing aren’t truly random but rather pseudorandom, meaning they’re generated using deterministic algorithms that simulate randomness. Java offers multiple approaches to random number generation, each suited for different purposes:
🌐
Built In
builtin.com › articles › math.random-java
Java Math.random() Method Explained With Examples | Built In
June 23, 2025 - It uses a shared Random instance ... more The Math.random() method in Java generates a pseudorandom double between 0.0 (inclusive) and 1.0 (exclusive). It uses a shared Random instance behind the scenes, making it simple but unsuitable for ...