-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRandomInt.java
More file actions
34 lines (29 loc) · 928 Bytes
/
RandomInt.java
File metadata and controls
34 lines (29 loc) · 928 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
29
30
31
32
33
34
/******************************************************************************
* Compilation: javac RandomInt.java
* Execution: java RandomInt N
*
* Prints a pseudo-random integer between 0 and N-1.
* Illustrate an explicit type conversion (cast) from double to int.
*
* % java RandomInt 6
* Your random integer is: 5
*
* % java RandomInt 6
* Your random integer is: 0
*
* % java RandomInt 1000
* Your random integer is: 129
*
* % java RandomInt 1000
* Your random integer is: 333
*
******************************************************************************/
public class RandomInt {
public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
// a pseudo-random real between 0.0 and 1.0
double r = Math.random();
int n = (int) (r * N);
System.out.println("You random integer is: " + n);
}
}