
Random Numbers in Java
Presentation
•
Computers
•
9th - 12th Grade
•
Medium
Lisa Donlan
Used 10+ times
FREE Resource
7 Slides • 8 Questions
1
Random Numbers in Java
by Lisa Donlan
2
3
How do random numbers work?
Math.random() returns a random number between 0.0-0.99.
(int)(Math.random( )*range) + min moves the random number into a range starting from a minimum number.
The range is the (max number - min number + 1).
4
What do you think the output is?
public class Test3
{
public static void main(String[ ] args)
{
System.out.println(Math.random());
System.out.println(Math.random());
}
}
5
One possible result is
0.3842967819081746
0.30067392084109024
System.out.println(Math.random());
System.out.println(Math.random());
6
Multiple Choice
What does
double rnd=Math.random( );
return?
returns a random number between 0.0-0.99
returns a random number between 0.0-1.00
returns a random number between 0 and 1
a negative number
7
How do you use random to choose numbers?
Example: Randomly choose numbers on a die.
​
8
Multiple Choice
int rnd2=(int)(Math.random( )∗10)+1;
System.out.print(rnd1);
What is the output?
any integer between 1 and 10 exclusive
any integer between 1 and 10 inclusive
any integer between 0 and 10 inclusive
any integer between 0 and 10 exclusive
9
Multiple Choice
How would you choose the numbers from 1 to 100?
System.out.println((int) (Math.random( ) * 10)+10);
System.out.println((int) (Math.random( ) * 10)+100);
System.out.println((int) (Math.random( ) * 100)+100);
System.out.println((int) (Math.random( ) * 100)+1);
10
How do you get a random number you can use?
// rnd1 is an integer in the range 0-9 (including 9).
int rnd1 = (int)(Math.random( ) * 10);
Explain to your shoulder partner
Why do we have to use (int)?
What is this called (the concept is tested on the AP test EVERY year!!)?
Why do we have to multiply by 10?
11
Multiple Choice
Which of the following would be true about 40% of the time?
Math.random() > 0.4
Math.random( ) < 0.4
Math.random() == 0.4
12
Multiple Choice
Which of the following would return a random number from 1 to 5 inclusive?
((int) (Math.random( ) * 5))
((int) (Math.random( ) * 6))
((int) (Math.random( ) * 5) + 1)
13
Multiple Choice
Which of the following would return a random number from 0 to 10 inclusive?
((int) (Math.random( ) * 11))
((int) (Math.random( ) * 10))
((int) (Math.random( ) * 10) + 1)
14
Multiple Choice
Which of the following would be true about 75% of the time?
Math.random( ) < 0.25
Math.random( ) == 0.25
Math.random( ) > 0.25
15
Multiple Choice
Which of the following statements assigns a random integer between 25 and 60, inclusive, to rn?
This is a released AP MC question.
int rn = (int) (Math.random( ) * 25) + 36;
int rn = (int) (Math.random( ) * 36) + 25;
int rn = (int) (Math.random( ) * 25) + 60;
int rn = (int) (Math.random( ) * 60) + 25;
int rn = (int) (Math.random( ) * 26) + 60;
Random Numbers in Java
by Lisa Donlan
Show answer
Auto Play
Slide 1 / 15
SLIDE
Similar Resources on Wayground
11 questions
Rational Exponents and Radicals
Presentation
•
10th - 12th Grade
11 questions
HA2 Adding and Subtracting Rational Expressions Practice
Presentation
•
9th - 11th Grade
11 questions
Kinematics Equations Matchup
Presentation
•
10th - 12th Grade
12 questions
Database Data Types
Presentation
•
9th - 12th Grade
10 questions
3.3 Quadratic Formula
Presentation
•
9th - 11th Grade
12 questions
Navigating Schoology Part B
Presentation
•
9th - 12th Grade
10 questions
Adding Rational Expressions
Presentation
•
10th - 12th Grade
9 questions
Jaringan Komputer
Presentation
•
9th - 12th Grade
Popular Resources on Wayground
5 questions
A Home on the Shore
Quiz
•
3rd Grade
28 questions
US History Regents Review
Quiz
•
11th Grade
6 questions
A Horse Tale
Quiz
•
3rd Grade
20 questions
Math Review
Quiz
•
3rd Grade
10 questions
Juneteenth History and Significance
Interactive video
•
5th - 8th Grade
20 questions
Dividing Fractions
Quiz
•
5th Grade
55 questions
A Long Walk to Water Final Review
Quiz
•
6th - 8th Grade
10 questions
Equation Word Problems
Quiz
•
7th Grade
Discover more resources for Computers
28 questions
US History Regents Review
Quiz
•
11th Grade
20 questions
Insurance
Quiz
•
9th - 12th Grade
10 questions
Juneteenth: History and Significance
Interactive video
•
7th - 12th Grade
10 questions
7.3-7.4 Quiz
Quiz
•
11th Grade
20 questions
Summer Geometry QUIZ (Week 2)
Quiz
•
9th Grade
6 questions
Mayan Mathematics part 1
Presentation
•
9th - 12th Grade
25 questions
Biology Regents Review
Quiz
•
9th Grade
40 questions
Flags of the World
Quiz
•
KG - Professional Dev...