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 Overflow
🌐
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 › java-util-random-class-java
Java.util.Random class in Java - GeeksforGeeks
July 23, 2025 - Random class is used to generate pseudo-random numbers in java. An instance of this class is thread-safe. The instance of this class is however cryptographically insecure. This class provides various method calls to generate different random ...
🌐
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 ...
🌐
CodeGym
codegym.cc › java blog › java classes › java.util.random class
Java.util.Random class
October 11, 2023 - The java.util.Random class is used to generate pseudorandom numbers. The methods implemented by this class are used to generate different random data types such as int, double, and float.
🌐
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:
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.util.random
Random Class (Java.Util) | Microsoft Learn
An instance of this class is used to generate a stream of pseudorandom numbers; its period is only 2<sup>48</sup>. [Android.Runtime.Register("java/util/Random", DoNotGenerateAcw=true)] public class Random : Java.Lang.Object, IDisposable, ...
🌐
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.
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
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 −
🌐
Medium
medium.com › buzz-code › java-8-random-class-import-b7d95a3d276e
Java 8 | Random Class + Import
December 29, 2020 - Java 8 | Random Class + Import Random — Random class is one of java.util package, that generates random numbers. Hi guys! Welcome to my blog. Like I told you last time, today I’ll be talking …
🌐
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 ...