-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRandomNumber.java
More file actions
48 lines (42 loc) · 2.13 KB
/
Copy pathRandomNumber.java
File metadata and controls
48 lines (42 loc) · 2.13 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
46
47
48
package RandomNumber;
import java.util.Scanner; // used to get user input
public class RandomNumber {
private static int max, min = 1;
public static void main(String[] args) {
if (args.length != 0) {
if (args.length == 1)
System.out.println("[->] Generated Random Number: " + generateRandom(min, Integer.parseInt(args[0])));
else if (args.length == 2)
System.out.println("[->] Generated Random Number: " + generateRandom(Integer.parseInt(args[0]), Integer.parseInt(args[1])));
else {
System.out.println("[!] Commandline Arguments must be: [max (Integer)]");
System.out.println(" \t\t OR ");
System.out.println("[!] Commandline Arguments must be: [min (Integer)], [max (Integer)] ");
}
} else {
Scanner scanner = new Scanner(System.in);
System.out.print("[<-] Enter the minimum value (inclusive): ");
min = scanner.nextInt();
System.out.print("[<-] Enter the maximum value (inclusive): ");
max = scanner.nextInt();
scanner.close(); // closing the stream as it's not needed anymore.
System.out.println("[->] Generated Random Number: " + generateRandom(min, max));
}
}
public static int generateRandom(int min, int max) {
System.out.println(min + " " + max);
// if min = max, then we cannot generate a random number in between so we return the same value.
if (min == max)
return min;
// the case where user gives min > max, for ex: min = 5, max = 2. Then we simply interchange values to proceed further.
// making min = 2 and max = 5.
else if (min > max) {
System.out.println("[!] Not sure if you tried to confuse me, but seems like the min value is greater than max.");
int tempVariable = min;
min = max;
max = tempVariable;
System.out.printf("[!] Generating a random number by taking min = %d and max = %d%n", min, max);
}
return (int) (Math.random() * (max - min + 1)) + min;
}
}