-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomNumber
More file actions
139 lines (128 loc) · 4 KB
/
Copy pathRandomNumber
File metadata and controls
139 lines (128 loc) · 4 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
Example1
import java.lang.Math;
public class RandomNumberExample1
{
public static void main(String args[])
{
// Generating random numbers
System.out.println("1st Random Number: " + Math.random());
System.out.println("2nd Random Number: " + Math.random());
System.out.println("3rd Random Number: " + Math.random());
System.out.println("4th Random Number: " + Math.random());
}
}
Example2
public class RandomNumberExample2
{
public static void main( String args[] )
{
int min = 200;
int max = 400;
//Generate random double value from 200 to 400
System.out.println("Random value of type double between "+min+" to "+max+ ":");
double a = Math.random()*(max-min+1)+min;
System.out.println(a);
//Generate random int value from 200 to 400
System.out.println("Random value of type int between "+min+" to "+max+ ":");
int b = (int)(Math.random()*(max-min+1)+min);
System.out.println(b);
}
}
Exmple3
import java.util.Random;
public class RandomNumberExample3
{
public static void main(String args[])
{
// creating an object of Random class
Random random = new Random();
// Generates random integers 0 to 49
int x = random.nextInt(50);
// Generates random integers 0 to 999
int y = random.nextInt(1000);
// Prints random integer values
System.out.println("Randomly Generated Integers Values");
System.out.println(x);
System.out.println(y);
// Generates Random doubles values
double a = random.nextDouble();
double b = random.nextDouble();
// Prints random double values
System.out.println("Randomly Generated Double Values");
System.out.println(a);
System.out.println(b);
// Generates Random float values
float f=random.nextFloat();
float i=random.nextFloat();
// Prints random float values
System.out.println("Randomly Generated Float Values");
System.out.println(f);
System.out.println(i);
// Generates Random Long values
long p = random.nextLong();
long q = random.nextLong();
// Prints random long values
System.out.println("Randomly Generated Long Values");
System.out.println(p);
System.out.println(q);
// Generates Random boolean values
boolean m=random.nextBoolean();
boolean n=random.nextBoolean();
// Prints random boolean values
System.out.println("Randomly Generated Boolean Values");
System.out.println(m);
System.out.println(n);
}
}
Example4
import java.util.concurrent.ThreadLocalRandom;
public class RandomNumberExample4
{
public static void main(String args[])
{
// Generate random integers between 0 to 999
int a = ThreadLocalRandom.current().nextInt();
int b = ThreadLocalRandom.current().nextInt();
// Print random integer values
System.out.println("Randomly Generated Integer Values:");
System.out.println(a);
System.out.println(b);
// Generate Random double values
double c = ThreadLocalRandom.current().nextDouble();
double d = ThreadLocalRandom.current().nextDouble();
// Print random doubles
System.out.println("Randomly Generated Double Values:");
System.out.println(c);
System.out.println(d);
// Generate random boolean values
boolean e = ThreadLocalRandom.current().nextBoolean();
boolean f = ThreadLocalRandom.current().nextBoolean();
// Print random Booleans
System.out.println("Randomly Generated Boolean Values:");
System.out.println(e);
System.out.println(f);
}
}
Example5
import java.util.Random;
public class RandomNumberExample5
{
public static void main(String[] args)
{
randomInts(5);
randomInts(9, 50, 90);
//getStreamOfRandomInts(30, 50);
}
//method that generates a stream of integers having size 5
public static void randomInts(int num)
{
Random random = new Random();
random.ints(num).forEach(System.out::println);
}
//method that generates a stream of 9 integers between 50 to 90
public static void randomInts(int num, int origin, int bound)
{
Random random1 = new Random();
random1.ints(num, origin, bound).forEach(System.out::println);
}
}