1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package uk.org.bryanray.testtoys.generator.sequencegenerators;
17
18 import java.text.DecimalFormat;
19 import java.util.List;
20 import java.util.Random;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 public class RandomNumberGenerator implements SequenceGenerator {
39 private DecimalFormat decimalFormat;
40 private double minValue;
41 private double maxValue;
42 private String currentValue;
43 private Random random = new Random();
44
45
46
47
48
49
50
51
52
53
54
55 public RandomNumberGenerator(List<String> args) {
56 if (3 == args.size()) {
57 String pattern = args.get(0);
58 if (null != pattern && !"".equals(pattern)) {
59 decimalFormat = new DecimalFormat(pattern);
60 } else {
61 decimalFormat = new DecimalFormat();
62 }
63
64 double minValue = Double.parseDouble(args.get(1));
65 double maxValue = Double.parseDouble(args.get(2));
66 if (maxValue >= minValue) {
67 this.minValue = minValue;
68 this.maxValue = maxValue;
69 next();
70 } else {
71 throw new IllegalArgumentException(
72 "Max value exceeds min value.");
73 }
74 } else {
75 throw new IllegalArgumentException(
76 "Incorrect arguments: minValue, maxValue.");
77 }
78 }
79
80
81
82
83 public void next() {
84 currentValue = decimalFormat.format(random.nextDouble()
85 * (maxValue - minValue) + minValue);
86 }
87
88
89
90
91 public String value() {
92 return currentValue;
93 }
94 }