0% found this document useful (0 votes)
3 views

Lesson-2.2

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lesson-2.2

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Random Number Generator

This generator simply produces a different number every time it is


referenced.

The generator uses the Java Random object.

This object is not built into the basic Java language.

It is stored in a Java API Package.

The program will be using java.util.Random from the API Package.

Use an import statement:

import java.util.Random;
This statement goes before the program’s class definition header.

The class must be imported before anything else is done in the


program.

To use the Random object, it is first created using the object


constructor:

Random myRandom = new Random();

This statement is placed with the variable declaration statements.

Whenever a random integer value is needed, use the nextInt


method of this Random object.

myRandom.nextInt(Limit)
This statement generates a random integer value that is greater
than or equal to 0 and less than Limit.

myRandom.nextInt(5)

This will generate a random number between 0 and 4.

myRandom.nextInt(11)

This will generate a random number between 0 and 10.

myRandom.nextInt(20)

This will generate a random number between 0 and 19.


Sample Program using Random Numbers

/*
* Random Test
* Beginning Java
*/
package randomtest;
import java.util.Random;

public class RandomTest


{
public static void main(String[] args)
{
Random myRandom = new Random();
System.out.println("Random number " + myRandom.nextInt(10));
}
}
The random number generator object can be used to introduce
randomness in a project.

Every computer game, video game, and computer simulation, like


sports games and flight simulators, use random numbers.

A roll of a dice can produce a number from 1 to 6.


For a deck of cards, the random integers would range from 1 to 52
since there are 52 cards in a standard playing deck.

To use our myRandom object to roll a dice, we would write:

diceNumber = myRandom.nextInt(6) + 1;

To use our myRandom object on a deck of cards, we would write:

cardNumber = myRandom.nextInt(52) + 1;

If we want a number between 0 and 100, we would use:

yourNumber = myRandom.nextInt(101)
Project - Guess the Number Game

The steps needed to do play this game are:

1. Computer picks a number between 1 and 10.


2. Computer asks your guess.
3. Computer analyzes your guess and outputs the result.
/*
* Guess the Number
* Beginning Java
*/
package number;
import java.util.Random;
import java.util.Scanner;
public class Number
{
public static void main(String[] args)
{
int computerNumber;
int yourGuess;
Random myRandom = new Random();
Scanner myScanner = new Scanner(System.in);
computerNumber = myRandom.nextInt(10) + 1;
System.out.println("I'm thinking of a number between 1 and 10.");

System.out.print("What do you think it is? ");


yourGuess = myScanner.nextInt();

if (yourGuess == computerNumber)
{
System.out.println("You got it!! That's my number!");
}

else if (yourGuess < computerNumber)


{
System.out.println("You are too low!! My number was " +
computerNumber);
}
else
{
// too high
System.out.println("You are too high!! My number was " +
computerNumber);
}
}
}
1. Create a program to guess the throw of a die. The user will enter
his guess and this will be followed by the actual result of the throw.
If the guess is the same as the side the message “You won!” will
be displayed, else “You lost!” will be shown.

2. Create a program that will add the result of three throws of die.
The user will guess the total and the result will be shown. If the
user is correct, “You Won !” will be shown, else “You Lost!” will be
displayed.

You might also like