CHAPTER 20 — The Random Class

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to home page   hear noise   go to next page

created: 07/31/07; revised 10/22/08

CHAPTER 20 — The Random Class

die

Programs frequently need random numbers. A card game uses random numbers to create a shuffled deck of cards. A dice game uses random numbers to simulate a throw. Many activities in the real world (such as traffic on a highway system) appear to be random, and a program that models them uses random numbers. This chapter discusses the Random class that comes with Java.

The chapter discusses:

  • Random numbers
  • Random number generators
  • Uniform and non-uniform distributions
  • The Random class
  • Seeds for Random()
  • Tossing dice
  • Picking passwords

Random numbers, probability, and statistics are important for testing, for modeling the world, and for general understanding of the universe. Computer science degree programs usually require a course in these subjects.

QUESTION 1:

What number is most likely outcome for a throw of a fair die?