Download presentation
Presentation is loading. Please wait.
Published byAlvin Farmer Modified over 8 years ago
1
Random UNPREDICTABLE NUMBERS
2
A FEW APPLICATIONS THAT USE RANDOM…. Predictions for life expectance used in insurance Business simulations Games (Give users a different experience each time) Simulations for scientific research Lottery
3
The Random Class Requires the import of java.util.Random Or java.util.*; Create an object of the random class Random randm = new Random( ); int x = randm.nextInt() generates a value between -2,147,843,648 to 2,147,843,648.
4
Let’s ask for a reasonable range int x = randm.nextInt(21); Generates a number between 0 and 20
5
What if we want a range between 90 to 110 First we subtract (110-90). Then add 1 to get 21. int r =90+randm.nextInt(21);
6
Now it’s your turn! Create a program to generate a secret number between 1 to 10. Prompt the user to guess the secret number until the correct number is guessed. Give the user hints: – If the user’s guess is greater than the secretNum tell the user the guess is “Too high. Guess again! ” – If the user’s guess is “Too low, Guess again!Guess Again!”
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.