The first solution is to use the java.util.Random class:
import java.util.Random;
Random rand = new Random();
// Obtain a number between [0 - 49].
int n = rand.nextInt(50);
// Add 1 to the result to get a number from the required range
// (i.e., [1 - 50]).
n += 1;
Another solution is using Math.random():
double random = Math.random() * 49 + 1;
or
int random = (int)(Math.random() * 50 + 1);
Answer from nyanev on Stack OverflowOracle
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 ...
Videos
Using the Random Class in Java - YouTube
04:27
Java random numbers 🎲 【4 minutes】 - YouTube
06:15
Java Programming Series Episode 41: The Random Class In Java - YouTube
05:01
Generate random numbers using Java! 🔀 - YouTube
05:01
JAVA || How to use the Random class WITH lower and upper bounds ...
DigitalOcean
digitalocean.com › community › tutorials › java-random
Java Random | DigitalOcean
August 4, 2022 - This class provides several methods to generate random numbers of type integer, double, long, float etc. Random number generation algorithm works on the seed value. If not provided, seed value is created from system nano time. If two Random instances have same seed value, then they will generate ...
Oracle
docs.oracle.com › javase › 7 › docs › api › java › util › Random.html
Random (Java Platform SE 7 )
Generates random bytes and places them into a user-supplied byte array. The number of random bytes produced is equal to the length of the byte array. The method nextBytes is implemented by class Random as if by:
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › util › Random.html
Random (Java SE 21 & JDK 21)
January 20, 2026 - The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald E. Knuth, The Art of Computer Programming, Volume 2, Third edition: Seminumerical Algorithms, Section 3.2.1.) 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.
Top answer 1 of 2
898
The first solution is to use the java.util.Random class:
import java.util.Random;
Random rand = new Random();
// Obtain a number between [0 - 49].
int n = rand.nextInt(50);
// Add 1 to the result to get a number from the required range
// (i.e., [1 - 50]).
n += 1;
Another solution is using Math.random():
double random = Math.random() * 49 + 1;
or
int random = (int)(Math.random() * 50 + 1);
2 of 2
626
int max = 50;
int min = 1;
1. Using Math.random()
double random = Math.random() * 49 + 1;
or
int random = (int )(Math.random() * 50 + 1);
This will give you value from 1 to 50 in case of int or 1.0 (inclusive) to 50.0 (exclusive) in case of double
Why?
random() method returns a random number between 0.0 and 0.9..., you multiply it by 50, so upper limit becomes 0.0 to 49.999... when you add 1, it becomes 1.0 to 50.999..., now when you truncate to int, you get 1 to 50. (thanks to @rup in comments). leepoint's awesome write-up on both the approaches.
2. Using Random class in Java.
Random rand = new Random();
int value = rand.nextInt(50);
This will give value from 0 to 49.
For 1 to 50: rand.nextInt((max - min) + 1) + min;
Source of some Java Random awesomeness.
Educative
educative.io › answers › how-to-generate-random-numbers-using-random-class-in-java
How to generate random numbers using Random class in Java
The Java Random class is a part of the java.util package and contains inbuilt methods to generate random numbers.
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › util › Random.html
Random (Java SE 11 & JDK 11 )
January 20, 2026 - Generates random bytes and places them into a user-supplied byte array. The number of random bytes produced is equal to the length of the byte array. The method nextBytes is implemented by class Random as if by:
TutorialsPoint
tutorialspoint.com › home › java/util › java random class
Java Random Class
September 1, 2008 - The algorithms implemented by class Random use a protected utility method that on each invocation can supply up to 32 pseudorandomly generated bits. Following is the declaration for java.util.Random class −
Rutgers CS
cs.rutgers.edu › courses › 111 › classes › fall_2011_tjang › texts › notes-java › algorithms › random › random-api.html
Java: Random numbers - API
Two classes. Java provides the Math.random() method as well as the java.util.Random class. The methods of the Random class often produce random numbers in a more convenient form, but requires creating an object, which sometimes is inconvenient. In constrast, the Math.random() method produces ...
GitHub
github.com › openjdk › jdk › blob › master › src › java.base › share › classes › java › util › Random.java
jdk/src/java.base/share/classes/java/util/Random.java at master · openjdk/jdk
import java.util.stream.LongStream; · import static jdk.internal.util.random.RandomSupport.*; · import jdk.internal.misc.Unsafe; · /** * An instance of this class is used to generate a stream of · * pseudorandom numbers; its period is only 2<sup>48</sup>. * The class uses a 48-bit seed, which is ·
Author openjdk
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])
December 20, 2025 - The principal interface is RandomGenerator, which provides methods for requesting individual values of type int, long, float, double, or boolean chosen pseudorandomly from a uniform distribution; methods for requesting values of type double chosen pseudorandomly from a normal distribution or from an exponential distribution; and methods for creating streams of values of type int, long, or double chosen pseudorandomly from a uniform distribution (such streams are spliterator-based, allowing for parallel processing of their elements). There are also static factory methods for creating an instance of a specific random number generator algorithm given its name. The principal supporting class is RandomGeneratorFactory.
Educative
educative.io › answers › how-to-generate-random-numbers-in-java
How to generate random numbers in Java
The ints is an instance method of the Random class that is used to generate a stream of random integers in Java 8.
W3Schools Blog
w3schools.blog › home › java random class tutorial
Java random class tutorial - w3schools.blog
March 6, 2025 - package com.w3schools; import java.util.Random; public class Test { public static void main(String args[]){ Random random = new Random(); //It generates boolean value System.out.println(random.nextBoolean()); //It generates double value System.out.println(random.nextDouble()); //It generates ...