-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsingRandom.java
More file actions
29 lines (23 loc) · 817 Bytes
/
UsingRandom.java
File metadata and controls
29 lines (23 loc) · 817 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class UsingRandom {
public static void main(String[] args) {
// Math.random() generates a pseudo-random number
// between 0 (inclusive) and 1 (exclusive):
// 0 <= Math.random() < 1
System.out.println(Math.random());
// What are the possible values for x?
// double 0.0 <= a < 30.0
double x = Math.random() * 30;
System.out.println("x: " + x);
// What are the possible values for y?
// int 0 <= b <= 29 (int)
int y = (int)(Math.random() * 30);
System.out.println("y: " + y);
// What are the possible values for z?
// int -15 <= c <= 14
int z = (int)(Math.random() * 30) - 15;
System.out.println("z: " + z);
// General formula for a random int x
// such that a <= x <= b
// int x = (int)(Math.random() * (b - a + 1)) + a
}
}