Random - Raffle numbers - Game: Guess a number


We will learn how to generate random numbers using Random objects. With the excuse of learning to use Random and practice what you learned in the previous tutorial, we will create our first game.

Random

The objects of the Random class, allows us to generate random numbers. After creating the Random object, we can ask the object for numbers, using different methods. The most used methods are:

Field seed

The Random objects have a field called seed. It can be modified using random.setSeed (long seed) or in the constructor Random (seed).

Normally, when we use random to generate sequences, they are not repeated. If, before we generate the sequence, we define the seed to a fixed value, it will always generate the same sequence.  

The use of seed, can be interesting when we want to test modifications with the same data.

package com.edu4java.javatutorials;
import java.util.Random;

public class RandomExample {
	public static void main(String[] args) {
		Random r = new Random(1); // sets the field seed to 1
		int i = 0; // it is a counter variable
		while (i < 20) {
			i++; // is the same as i = i + 1;
			System.out.print(r.nextInt(100) + ","); 
		}
	}
}

 

We are using the constructor new Random (1), so the sequence is always the same:

85,88,47,13,54,4,34,6,78,48,69,73,17,63,62,34,92,62,96,89,

If we change the constructor for Random (), you will see that it changes with each execution.

 

Game: Guess a number

In the following example, we create a game to guess a number with what we have learned so far.

package com.edu4java.javatutorials;

import java.util.Random;
import javax.swing.JOptionPane;

/**
 * @author edu4java
 * 
 *         The program raffles a number between 0 and 99 and asks the user repeatedly to
 *         guess a number. Each time the user is wrong, the program tells the user if the
 *         number is higher or lower. When the user succeds, the programs tells the user how many attempts he failed.
 */
public class GameGuess {
	public static void main(String[] args) {
		Random r = new Random();
		int numberToGuess = r.nextInt(100); // generates a number between 0 and 99
		int i = 0; // creates a counter variable used to count the failed attempts
		int bet = input("Guess the secret number between 0 and 99. What number is it?");
		while (numberToGuess != bet) { // if the number given is different from the raffled one it repeats the loop
			i++; // counter variable increments 1
			if (numberToGuess > bet) {
				bet = input("The number is higher. \n Try again");
			} else {
				bet = input("The number is lower \n Try again");
			}
		}
		JOptionPane.showMessageDialog(null,
				"Congratulations! you`ve guessed the number with " + i + " failed attempts");
	}

	/**
	 * Shows a window with the text message and asks for an answer
	 * 
	 * @param text
	 * @return int number
	 */
	private static int input(String text) {
		return Integer.parseInt(JOptionPane.showInputDialog(text));
	}
}

The input(String text) method has been created to make the code more readable.  

The code is documented (explained) in the same program. This is a very good habit.