-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path123.java
More file actions
193 lines (168 loc) · 4.95 KB
/
Copy path123.java
File metadata and controls
193 lines (168 loc) · 4.95 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*******************************************************************************
* Copyright 2016, 2017 vanilladb.org contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package org.vanilladb.bench.util;
import java.util.Random;
/**
* A thread-safe random value generator.
*
* @author SLMT
*
*/
public class RandomValueGenerator {
private static final String CHARSET = new String(
"QAa0bcLdUK2eHfJgTP8XhiFj61DOklNm9nBoI5pGqYVrs3CtSuMZvwWx4yE7zR");
private Random rng;
public RandomValueGenerator() {
rng = new Random();
}
public RandomValueGenerator(long seed) {
rng = new Random(seed);
}
public Random rng() {
return (rng);
}
/**
* Return the result of a random choose from a given distribution.
*
* @param probs
* @return
*/
public int randomChooseFromDistribution(double... probs) {
int result = -1;
int[] range = new int[probs.length];
double accuracy = 1000;
int total = 0;
for (int i = 0; i < probs.length; i++) {
range[i] = (int) (probs[i] * accuracy);
total += range[i];
}
int randNum = (int) (rng.nextDouble() * total);
for (int i = 0; i < range.length; i++) {
randNum -= range[i];
if (randNum <= 0) {
result = i;
break;
}
}
return result;
}
/**
* Return an integer in the (inclusive) range [min, max].
*/
public int number(int min, int max) {
if (min > max)
throw new IllegalArgumentException();
int value = rng.nextInt(max - min + 1);
value += min;
return value;
}
/**
* Return a long in the (inclusive) range [min, max].
*/
public long randomLongRange(long min, long max) {
if (max <= min)
return min;
long width = max - min;
return (long) (rng.nextDouble() * width) + min;
}
/**
* Return a double in the (inclusive) range [min, max], with the increase
* precision.
*/
public double randomDoubleIncrRange(double min, double max, double incr) {
long width = (long) ((max - min) / incr);
return randomLongRange(0, width) * incr + min;
}
/**
* Return an integer in the (inclusive) range [minimum, maximum] excluding
* exclusivedVal.
*/
public int numberExcluding(int min, int max, int exclusivedVal) {
if (min > max)
throw new IllegalArgumentException();
if (min > exclusivedVal || exclusivedVal > max)
throw new IllegalArgumentException();
int value = number(min, max - 1);
if (value >= exclusivedVal)
value++;
return value;
}
/**
* Return an fixed decimal double value in the (inclusive) range [minimum,
* maximum]. For example, [0.01 .. 100.00] with decimal 2 has 10,000 unique
* values.
*
*/
public double fixedDecimalNumber(int decimal, double min, double max) {
if (min > max)
throw new IllegalArgumentException();
if (decimal < 0)
throw new IllegalArgumentException();
int multiplier = 1;
for (int i = 0; i < decimal; ++i) {
multiplier *= 10;
}
int top = (int) (min * multiplier);
int bottom = (int) (max * multiplier);
return (double) number(top, bottom) / (double) multiplier;
}
/**
* Return a string of random alphanumeric characters of a random length
* between [minLength, maxLength].
*/
public String randomAString(int minLength, int maxLength) {
int length = number(minLength, maxLength);
return randomAString(length);
}
public String randomAString(int length) {
StringBuffer sb = new StringBuffer();
int te = 0;
for (int i = 1; i <= length; i++) {
te = rng().nextInt(62);
sb.append(CHARSET.charAt(te));
}
return sb.toString();
}
/**
* @return a random numeric string with length in range [minimum_length,
* maximum_length].
*/
public String nstring(int minimum_length, int maximum_length) {
return randomString(minimum_length, maximum_length, '0', 10);
}
public String nstring(int length) {
return randomString(length, '0', 10);
}
public String randomZipCode() {
StringBuffer sb = new StringBuffer();
sb.append(nstring(4));
sb.append("11111");
return sb.toString();
}
private String randomString(int minimum_length, int maximum_length,
char base, int numCharacters) {
int length = number(minimum_length, maximum_length);
return randomString(length, base, numCharacters);
}
private String randomString(int length, char base, int numCharacters) {
byte baseByte = (byte) base;
byte[] bytes = new byte[length];
for (int i = 0; i < length; ++i) {
bytes[i] = (byte) (baseByte + number(0, numCharacters - 1));
}
return new String(bytes);
}
}