forked from nrkapri/JavaAdvancedTopics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomNumberexample.java
More file actions
45 lines (29 loc) · 1.24 KB
/
Copy pathRandomNumberexample.java
File metadata and controls
45 lines (29 loc) · 1.24 KB
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
35
36
37
38
39
40
41
42
43
44
45
package java8;
import java.util.Random;
public class RandomNumberexample {
public static void main(String [] args)
{
//java.util.Random
System.out.println("java.util.Random:");
//Random().nextInt(int bound) = Random integer from 0 (inclusive) to bound (exclusive)
//1. nextInt(range) = nextInt(max - min)
System.out.println(new Random().nextInt(5));
//2. To include the last value (max value) = (range + 1)
System.out.println(new Random().nextInt(5 + 1) );// [0...5] [min = 0, max = 5]
//3. To define a start value (min value) in a range,
System.out.println(new Random().nextInt(5 + 1) + 10 );// [0...5] + 10 = [10...15]
//4. Test [10...30]
System.out.println(new Random().nextInt((99 - 15) + 1) + 15);
//Math.random
System.out.println("Math.random");
int max= 100, min =10;
System.out.println((int)(Math.random() * ((max - min) + 1)) + min);
//3. Java 8 Random.ints
System.out.println(" Java 8 Random.ints");
Random r = new Random();
System.out.println( r.ints(min, (max + 1)).findFirst().getAsInt());
//Java 8 only
System.out.println("Java 8 Random.ints stream");
new Random().ints(10, 99, 555).forEach(System.out::println);
}
}