Random Number Generation

There are several ways to generate random numbers in Java. We will use the method that utilized the Math library.

This will require you to include the following line of code at the top of your program, before your Lab4d class declaration.

import java.lang.Math;

The method, Math.random(), will return a random value between 0 (inclusively) and 1.0 (exclusively).

In order to modify this to a range between the integers 1 and some upper limit, we need to multiply the return value of Math.random() by the upper limit of the range we desire and add 1.

For example, if we wanted to generate a random number between 1 and 50, we would use the code:

double randNum = (Math.random() * 50) + 1;

If we wanted to limit the result further to only include integer values we would cast the results to ints.

int randNum = (int)(Math.random() * 50) + 1;

To make this code more useful and specify both an upper limit and a lower limit, we would modify the code as follows:

int randNum = (int)(Math.random() * (upperLimit - lowerLimit + 1) )+ lowerLimit;

For example, if we wanted to generate a random number between 17 and 1034, we would use the code:

int randNum = (int)(Math.random() * (1034 - 17 + 1)) + 17;

or more generically:

//Define the range
int max = 1034;
Value int min = 17;
int range = max - min + 1;

//Generate a random number between min and max
int randNum = (int)(Math.random() * range) + min;

Program: Guessing Game

For this lab, you’ll need to use do-while loops and if statements to construct a guessing game. The computer will choose a random number between 1 and 100 and the user will need to guess what the number is. If the user guesses incorrectly, the computer will indicate whether the user’s guess was too high or too low. If the user guesses correctly, the computer reports how many tries it took to get the correct answer, and then asks if the user would like to play again.

Technical Design:

This program will require four do-while loops:

The primary do-while loop (outermost) will contain almost all of the code in the program and keep running over and over until the user indicates they no longer wish to play.

The game do-while loop is nested inside the primary do-while loop and will keep running over and over again until the user guesses the correct answer and wins the game. As soon as the user wins a game, the program will exit this do-while loop.

The numeric input validation do-while loop is nested inside of the game do-while loop and will ensure that the user entered a valid whole number for each guess.

The non-numeric input validation do-while loop is located AFTER and OUTSIDE of the game do-while loop. It is only executed after a game is complete and asks the user if they would like to play again.

It is nested inside of the primary do-while loop and will ensure that the user entered a valid ‘Y’ / ‘y’ / ‘N’ / ‘n’ in answer to the “Would you like to play again (Y/N)?” question.

Big Hint:

When approaching a problem with several nested loops, it is sometimes easier to start with the innermost loops and work outward:

  • Create the numeric input validation loop
  • Indent the numeric input validation loop and surround it with the game loop
  • Add the non-numeric input validation loop after the game loop
  • Indent the game loop and the non-numeric input validation loops and surround them with the primary program loop

The diagram provides a rudimentary architecture for the guessing game program:

Key Program Requirements:

At the beginning of the program, output a message to the user describing what the program will do as shown in the example run.

Perform numeric input validation to ensure the user is entering a valid whole number.

Perform non-numeric input validation to ensure the user is entering a ‘Y’, ‘y’, ‘N’, or ‘n’ when asked if they want to play again.

Example Run:

This program is a guessing game.
The computer will generate a random integer between 1 and 100. The user will try to guess the number.
Let’s get started!

I’m thinking of a number between 1 and 100.
What is your guess? something
Error: Please enter a whole number.

What is your guess? 50
Your guess is too high. Try again.

What is your guess? 25
Your guess is too low. Try again.

What is your guess? 33
CORRECT! You guessed it in 3 tries!!

Would you like to play again? Not sure
Error: Please answer with a ‘Y’ or ‘N’.

Would you like to play again? y

This program is a guessing game.
The computer will generate a random integer between 1 and 100. The user will try to guess the number.
Let’s get started!

I’m thinking of a number between 1 and 100.
What is your guess? 50
Your guess is too high. Try again.

What is your guess? 25
Your guess is too high. Try again.

What is your guess? 12
Your guess is too high. Try again.

What is your guess? 6
Your guess is too high. Try again.

What is your guess? 3
Your guess is too low. Try again.

What is your guess? 4
CORRECT! You guessed it in 6 tries!

Would you like to play again? n

Struggling with Java homework? Get expert help and ace your Java assignments! and learn how to generate random numbers in Java with ease.

Our experienced Mentors/experts can guide you through the process and help you customize the built-in random number generator to fit your specific needs.

Don’t wait, improve your Java programming skills today and achieve your academic goals!