Random 2
Random 2
random()
OBJECTIVE
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
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.