© Doug Winnie 2021
D. WinnieEssential Java for AP CompScihttps://doi.org/10.1007/978-1-4842-6183-5_21

21. Random Numbers

Doug Winnie1  
(1)
Mission Hills, KS, USA
 

Rolling a die involves math.

Right? Why would someone ever say that?

Well, in Java and basically in programming, it does. Fortunately, the work is mostly behind the scenes, but we will need to do some work with variables and value types to get it all to work the way we want it.

Create a Random Number Generator

Working with random numbers requires working with a special class called the Random class.

When we work with some classes, like Math, we can just work with the class name and a method name after it. This is called a static class. We don’t need to do anything special with it. There is a ...

Get Essential Java for AP CompSci: From Programming to Computer Science now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.