How can I generate random integers in a specific range with Java?

Learn how to limit your random number generation to a specific range while using java.util.Random, Math.random, and ThreadLocalRandom.

By Brian L. Gorman
September 8, 2016

Programmers often need to generate random numbers, be it for a dice roll, choosing a random ID, or picking a sample value for testing purposes. In this tutorial Brian Gorman outlines three common approaches to random number generation and explains when each will prove most useful. After watching this video, Java beginners will be able to add java.util.Random, Math.random, and ThreadLocalRandom to their repertoire and limit their output to a specific range. Brian will take you step-by-step through each method, demonstrating the expected output of each process.

Brian L. Gorman is an experienced .NET developer, certified as an MCSD in C# .NET. Holding a Masters of Science in computer information systems, he has many years of experience instructing college-level SQL database, C#, VB.NET, Java, and Microsoft Office courses. Brian has also authored a number of full technical video courses on these subjects.

Learn faster. Dig deeper. See farther.

Join the O'Reilly online learning platform. Get a free trial today and find answers on the fly, or master something new and useful.

Learn more
Post topics: Software Engineering
Post tags: Questions

Get the O’Reilly Radar Trends to Watch newsletter