W3Schools Java Random Number Generation Tutorial Examples

Robert Jul 13, 2026

Welcome to our in-depth exploration of Random Number Generation in Java. If you're new to programming or Java, you might be wondering why random numbers are so important. The simple answer lies in their wide range of applications, from games and simulations to cryptography and machine learning algorithms. In this article, we'll guide you through the process of creating, using, and understanding random numbers in Java.

dumb coder
dumb coder

Before we dive in, let's talk about why understanding random number generation is crucial. Random numbers are essential for creating unpredictable outcomes, enhancing security, and adding an element of chance in your programs. So, whether you're a seasoned developer or a newcomer to Java, this guide will provide valuable insights into this fundamental aspect of programming.

a black and white graduation hat holding a red book with a cup of coffee in it
a black and white graduation hat holding a red book with a cup of coffee in it

Understanding Random Numbers in Java

In Java, the java.util.Random class is the primary tool for generating random numbers. It provides methods for generating random doubles, floats, longs, ints, and booleans. Before we start generating random numbers, let's first import the necessary class:

a red poster with the words sintaxis basica de java
a red poster with the words sintaxis basica de java

import java.util.Random;

The Basic Random Number Generator

a computer screen with the words public class main
a computer screen with the words public class main

Creating a random number generator in Java is as simple as creating a new instance of the java.util.Random class.

Random randomGenerator = new Random();

The More Predictable Random Class

java programs
java programs

However, Java also provides the java.util.Random

Random randomGenerator = new Random(10);

Random Number Generation Methods

Java
Java

The java.util.Random class provides various methods, like nextInt(), nextDouble(), nextFloat(), and nextBoolean(), to generate random numbers of different data types. Let's explore some practical examples.

Generating Random Integers

an image of a computer screen with many lines on the screen and text below it
an image of a computer screen with many lines on the screen and text below it
Java Programming Language
Java Programming Language
Programming meme
Programming meme
Java
Java
an image of some type of project ideas
an image of some type of project ideas
CSS W3 Course
CSS W3 Course
the words it's ok to like both java script and pyrton on a white background
the words it's ok to like both java script and pyrton on a white background
Top Features of Java Every Beginner Should Know โ˜•๐Ÿš€
Top Features of Java Every Beginner Should Know โ˜•๐Ÿš€
Best Java IDEs in 2020
Best Java IDEs in 2020
core java classes in Pune
core java classes in Pune
a computer screen with multiple lines of code on it's side and another line of code on the other side
a computer screen with multiple lines of code on it's side and another line of code on the other side
Java
Java
Learn Java in 30 Days Only
Learn Java in 30 Days Only
Top 5 Java Projects for Beginners
Top 5 Java Projects for Beginners
two different types of words that appear to be in the same language, one is for each
two different types of words that appear to be in the same language, one is for each
an anime character holding up a book above his head in a classroom with tables and chairs
an anime character holding up a book above his head in a classroom with tables and chairs
Java Scanner Class Made Easy: Your Ultimate Input Cheat Sheet ๐ŸŽฏ๐Ÿ’ป
Java Scanner Class Made Easy: Your Ultimate Input Cheat Sheet ๐ŸŽฏ๐Ÿ’ป
Learn Java in 30 Days Only
Learn Java in 30 Days Only
an orange and pink background with text on it
an orange and pink background with text on it
Java Exception Handling Made Simple
Java Exception Handling Made Simple

To generate a random integer, you can use the nextInt() method. This method takes an argument representing the maximum value (exclusive). Here's an example that generates a random integer between 0 and 99.

int randomNumber = randomGenerator.nextInt(100);

Generating Random Doubles and Floats

The nextDouble() and nextFloat() methods generate random double and float values respectively, within the range [0.0, 1.0). Notice how we can adjust this range by multiplying and adding to the generated number.

double randomNumber = randomGenerator.nextDouble() * 100;

Finally, remember to close the Random instance when you're done using it to avoid memory leaks. In reality, this might not be necessary unless you're dealing with millions of instances:

randomGenerator.close();

Random number generation in Java is indeed a simple task. However, understanding the underlying mechanisms and methods can help you craft more robust and secure applications. It's time to apply what you've learned and start generating those random numbers! Happy coding!