-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomGenerator.java
More file actions
76 lines (44 loc) · 2.29 KB
/
Copy pathRandomGenerator.java
File metadata and controls
76 lines (44 loc) · 2.29 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
import java.util.Random;
import java.math.BigDecimal;
import java.math.RoundingMode;
// *****************************************************************************
// *****************************************************************************
// Class: RandomGenerator
// Description: Class for generating random values in a specified range
class RandomGenerator {
static Random r = new Random();
// *************************************************************************
// Method: generateRandomInteger
// Description: Generates a random Integer value
// in the range [min, max]
// Parameters: min - minimum value (inclusive)
// max - maximum value (inclusive)
// Returns: Randomly generated Integer value
// Calls: Nothing
// Globals: r
static int generateRandomInteger (int min, int max) {
int range = max - min + 1;
return r.nextInt(range) + min;
}
// *************************************************************************
// Method: generateRandomDouble
// Description: Generates a random Double value
// in the range [min, max]
// with the specified amount of precision
// Parameters: min - minimum value (inclusive)
// max - maximum value (inclusive)
// precision - # of desired decimal places
// Returns: Randomly generated Double value
// Calls: Nothing
// Globals: r
static double generateRandomDouble (double min, double max, int precision) {
double range = max - min;
double value = ( range * r.nextDouble() ) + min;
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(precision, RoundingMode.HALF_UP);
return bd.doubleValue();
}
// *************************************************************************
}
// *****************************************************************************
// *****************************************************************************