[go: up one dir, main page]

0% found this document useful (0 votes)
7 views9 pages

Random 2

The document explains how to generate random numbers using the Math.random() method in Java, which returns a double value between 0.0 and less than 1.0. It provides examples of generating random numbers within specific ranges, such as between 0-99 and 10-20, as well as creating a random password. Additionally, it outlines the requirements for a guessing game program that allows users to guess a randomly generated number between 1 and 10 with a maximum of three attempts.

Uploaded by

hearo158
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views9 pages

Random 2

The document explains how to generate random numbers using the Math.random() method in Java, which returns a double value between 0.0 and less than 1.0. It provides examples of generating random numbers within specific ranges, such as between 0-99 and 10-20, as well as creating a random password. Additionally, it outlines the requirements for a guessing game program that allows users to guess a randomly generated number between 1 and 10 with a maximum of three attempts.

Uploaded by

hearo158
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

17.0 Math.

random()
OBJECTIVE

 Learn how to generate random numbers.


Math.random()

 This method returns a double value between 0.0 to less


than 1.0.
 Syntax: doubleVariable = Math.random()

Example:
double randNum = Math.random();
//randNum can be any value between 0.0 to 0.99…
Random numbers between 0 - 1
double rand; Possible
output:
String randStr; 0.825
for (int i = 0; i < 10; i++) { 0.172
rand = Math.random(); 0.437
randStr = String.format(“%.3f", 0.212
0.673
rand); 0.408
System.out.println(randStr); 0.650
} 0.898
0.424
Generate number from 0 – any value

int num = (int) (Math.random() * 100);

The code above generate number from 0 - 99


Generate number from minimum to maximum number

int min = 10;


int max = 20;
int num = (int) (Math.random() * (max – min + 1)
) + min;

The code above generate number from 10 – 20


Random numbers between 1 - 6
double rand; Possible
output:
int dice; 1
for (int i = 0; i < 10; i++) { 4
rand = Math.random(); 2
dice = (int)(1 + (rand * 6)); 3
3
System.out.println(dice); 6
} 6
6
2
Generate password
double rand;
int arrayIndex = 0;
String alphanumeric = "abcdefghijklmnopqrstuvwxyz0123456789";
String password= "";
for (int i = 0; i < 8; i++) {
rand = Math.random();
arrayIndex = (int)(rand * alphanumeric.length());
password += alphanumeric.charAt(arrayIndex);
}
System.out.println("Password: " + password);

Possible output:
Password: 9mvcfgy5
Direction: Make a guessing game program that
Lastname_3rd_Activity3 will generate a random number and ask the user to
guess the generated value with at most three (3)
attempts.
The system should have the corresponding
features:
1. Generate a random value between 1 to 10.
2. Display an error message when submitting
empty value.
3. Give the user at most 3 attempts to input a
value between 1 to 10.
4. Display “Higher” or “Lower” depending on the
user’s input compared to the generated value.
5. Display “Sorry, 3 attempts already.” after 3
incorrect attempts and “Congratulations! You
got it in N attempt/s”, if correct
6. Reset the game when game is over (win or
lose). Generate new random number then reset
attempt.

You might also like