You call int result = random.nextInt(101) which creates uniformly distributed integers in [0,100], which can take 101 different values. If you check if (result > 49) then you have 51 possible values ([50,100]) and in the else case you have only 50 values ([0,49]). Thus the result is more likely to be in the upper part. To fix it you can do int result = random.nextInt(100).

Answer from Michael Kreutz on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Random.html
Random (Java Platform SE 8 )
1 week ago - Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. The general contract of nextInt is that one int value in the specified range is pseudorandomly generated and returned.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-util-random-nextint-java
Java.util.Random.nextInt() in Java - GeeksforGeeks
March 21, 2025 - ... // Java Program to generate ... main(String[] args) { // create an object of Random class Random r = new Random(); // Generate random integers without any bounds System.out.println("Random number: " + r.nextInt()); } ...
🌐
Reddit
reddit.com › r/java › performance of random.nextint(n)
r/java on Reddit: Performance of Random.nextInt(n)
June 30, 2014 -

Today I find myself in an interesting position where half of my program's performance is being hogged by nextInt. Therefor I would like to share a bit of my findings on the subject in hopes it might be of interest to others.

Interestingly enough, Java divides the method of finding a random integer into two separate procedures based off of the passed maximum.

IF the number is a power of 2, as calculated below:

(n & -n) == n

A quick procedure is preformed. If, as calculated above, the number is a power of two, Java uses very little calculation power in order to find the requested random number.

However, if the maximum is not a power of 2, the operation speed varies greatly depending on the current seed.

🌐
Home and Learn
homeandlearn.co.uk › java › java-random-numbers.html
Java Random Number methods - nextInt
So, you import the Random class from java.util. You create a new Random object. Use nextInt after your random object and type a number between the round brackets of this method. The code above has a value of 10. This gets you a random number between 0 and 9.
Find elsewhere
🌐
Educative
educative.io › answers › how-to-generate-random-numbers-using-random-class-in-java
How to generate random numbers using Random class in Java
import java.util.Random; The most frequently used built-in methods for generating random numbers, are the following: nextInt(): Returns a random int value within the range: $ -2,147,483,648<=value<= 2,147,483, 647$ nextInt(int range): Returns ...
🌐
W3Schools
w3schools.com › java › java_howto_random_number.asp
Java How To Generate Random Numbers
clear() clone() compute() computeIfAbsent() computeIfPresent() containsKey() containsValue() entrySet() forEach() get() getOrDefault() isEmpty() keySet() merge() put() putAll() putIfAbsent() remove() replace() replaceAll() size() values() Java Scanner Methods · close() delimiter() findInLine() findWithinHorizon() hasNext() hasNextBoolean() hasNextByte() hasNextDouble() hasNextFloat() hasNextInt() hasNextLine() hasNextLong() hasNextShort() locale() next() nextBoolean() nextByte() nextDouble() nextFloat() nextInt() nextLine() nextLong() nextShort() radix() reset() useDelimiter() useLocale() useRadix() Java File Methods Java FileInputStream Java FileOutputStream Java BufferedReader Java BufferedWriter Java Iterator Methods Java Collections Methods Java System Methods Java Errors & Exceptions ·
🌐
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.
🌐
Java
download.java.net › java › early_access › panama › docs › api › java.base › java › util › random › package-summary.html
java.util.random (Java SE 19 & JDK 19 [build 1])
This strategy is supported by the interface RandomGenerator.SplittableGenerator. In this package, implementations of this interface include "L32X64MixRandom", "L64X128StarStarRandom", "L64X128MixRandom", "L64X256MixRandom", "L64X1024MixRandom", "L128X128MixRandom", "L128X256MixRandom", and "L128X1024MixRandom"; note that the class SplittableRandom also implements this interface. The structure of the central nextLong (or nextInt) method of an LXM algorithm follows a suggestion in December 2017 by Sebastiano Vigna that using one Linear Congruential Generator (LCG) as a first subgenerator and one Xor-Based Generator (XBG) as a second subgenerator (rather than using two LCG subgenerators) would provide a longer period, superior equidistribution, scalability, and better quality.
🌐
TutorialsPoint
tutorialspoint.com › java › util › random_nextint_inc_exc.htm
java.util.Random.nextInt() Method
Following is the declaration for java.util.Random.nextInt() method. public int nextInt(int n) n − This is the bound on the random number to be returned. Must be positive. The method call returns a pseudorandom, uniformly distributed int value ...
🌐
CodeGym
codegym.cc › java blog › methods in java › java random nextint() method
Java Random nextInt() Method
October 11, 2023 - int nextInt(int n) — returns the next random value of type int in the range from 0 to n.
🌐
Softhints
softhints.com › java-random-generator-nextint-example
Java Random Generator nextInt Examples - Softhints
February 14, 2018 - package number; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; public class Randoma { public static final void main(String... aArgs) { //using single Random Random randomGenerator = new Random(); ArrayList<Integer> items = new ArrayList<Integer>(1000); //generating 1000 random numbers from 1 to 100 for (int idx = 1; idx <= 1000; ++idx) { int randomInt = randomGenerator.nextInt(100); System.out.println(randomInt); items.add(randomInt); } //test randomness and print the result count(items); } // group count the list items and print them private static void count(List<Integer> items) { Map<Integer, Long> result = items.stream().collect( Collectors.groupingBy( Function.identity(), Collectors.counting() ) ); //print the number and how much times is generated result.forEach((item, value) -> System.out.println(item + " - " + value)); } }
🌐
Java Mex
javamex.com › tutorials › random_numbers
Generating random numbers in Java: the Java random class and beyond
There are many applications where we need to generate numbers that appear to be "random" or unpredictable. At its simplest, we can generate a random number in Java with a line of code such as the following: int diceRoll = 1 + ThreadLocalRandom.current().nextInt(6);
🌐
Baeldung
baeldung.com › home › java › java numbers › generating random numbers in java
Generating Random Numbers in Java | Baeldung
January 8, 2024 - RandomDataGenerator randomDataGenerator = new RandomDataGenerator(); int randomWithRandomDataGenerator = randomDataGenerator.nextInt(min, max); Certainly, this is one of the fastest random number generator implementations. It has been developed at the Information Sciences Department of the Milan University. The library is also available at Maven Central repositories. So, let’s add the dependency: <dependency> <groupId>it.unimi.dsi</groupId> <artifactId>dsiutils</artifactId> <version>2.6.0</version> </dependency> This generator inherits from java.util.Random.
🌐
DEV Community
dev.to › rock_win_c053fa5fb2399067 › mastering-random-in-java-a-complete-guide-for-developers-jlo
🎲 Mastering `Random` in Java: A Complete Guide for Developers - DEV Community
June 13, 2025 - int min = 50; int max = 100; int result = random.nextInt((max - min) + 1) + min; Faster and more efficient in multithreaded environments. import java.util.concurrent.ThreadLocalRandom; int randomNum = ThreadLocalRandom.current().nextInt(1, 101); // 1 to 100
🌐
Sentry
sentry.io › sentry answers › java › generating random numbers in java
Generating random numbers in Java | Sentry
September 15, 2024 - The nextInt method can also be called without an argument, causing it to return any valid integer (including negative integers). To generate a random number in a range between a given start and end number: Use the end - start of your range as ...
🌐
openHAB Community
community.openhab.org › setup, configuration and use › beginners
[solved] Random numbers not so random? (new java.util. ...
October 12, 2015 - Hello, Using this line, it seems i can generate a number between 0 and 5. randomNum = (new java.util.Random).nextInt(6) but I keep getting 5, 5, 5, 5, 0, 0, 4, 4, 4, 5, 5, 5, 4, 4, 5, 5, 5… Or something like that. …
🌐
Oreate AI
oreateai.com › blog › unlocking-randomness-in-java-your-friendly-guide-to-the-random-class › 2ef1dd695d95ca907e657a5e48872f7e
Unlocking Randomness in Java: Your Friendly Guide to the `Random` Class - Oreate AI Blog
January 28, 2026 - Let's peek at a few of the most common ones: nextInt(): This is your basic integer generator. Call it without any arguments, and you'll get a random integer across the entire int range (from Integer.MIN_VALUE to Integer.MAX_VALUE).
🌐
AlphaCodingSkills
alphacodingskills.com › java › notes › java-random-nextint.php
Java Random nextInt() Method - AlphaCodingSkills
The java.util.Random.nextInt() method returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence. Syntax: ...