KaiquanMah commited on
Commit
5c6ac75
·
verified ·
1 Parent(s): 3b35b78

Random.nextInt(upToButNotIncl)

Browse files
Week 3: Objects, files and exceptions/13a. Using existing libraries ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Java API = Java's ready-made libraries
2
+ https://docs.oracle.com/en/java/javase/11/docs/api/index.html
3
+
4
+
5
+
6
+
7
+ Random class
8
+ nextInt(int maximum) returns a random number between [0, maximum)
9
+ https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Random.html
10
+
11
+
12
+ import java.util.Random;
13
+
14
+ public class Example {
15
+ public static void main(String[] args){
16
+ Random rnd = new Random();
17
+ for (int i=0; i<5; i++) {
18
+ System.out.println(rnd.nextInt(6) + 1);
19
+ }
20
+ }
21
+ }
22
+
23
+ Example output:
24
+ 5
25
+ 4
26
+ 3
27
+ 4
28
+ 6
29
+
30
+
31
+
32
+
33
+
34
+
35
+
36
+
37
+