|
| 1 | +/* |
| 2 | + * Copyright 2023 by OLTPBenchmark Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | + |
| 19 | + package com.oltpbenchmark.util; |
| 20 | + |
| 21 | + import java.util.concurrent.ThreadLocalRandom; |
| 22 | + |
| 23 | + public class ThreadLocalRandomGenerator { |
| 24 | + |
| 25 | + /** |
| 26 | + * Constructor |
| 27 | + * |
| 28 | + * @param seed |
| 29 | + */ |
| 30 | + public ThreadLocalRandomGenerator() { |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Returns a random int value between minimum and maximum (inclusive) |
| 35 | + * |
| 36 | + * @param minimum |
| 37 | + * @param maximum |
| 38 | + * @returns a int in the range [minimum, maximum]. Note that this is inclusive. |
| 39 | + */ |
| 40 | + public int number(int minimum, int maximum) { |
| 41 | + |
| 42 | + int range_size = maximum - minimum + 1; |
| 43 | + int value = ThreadLocalRandom.current().nextInt(range_size); |
| 44 | + value += minimum; |
| 45 | + |
| 46 | + return value; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Returns a random long value between minimum and maximum (inclusive) |
| 51 | + * |
| 52 | + * @param minimum |
| 53 | + * @param maximum |
| 54 | + * @return |
| 55 | + */ |
| 56 | + public long number(long minimum, long maximum) { |
| 57 | + |
| 58 | + long range_size = (maximum - minimum) + 1; |
| 59 | + |
| 60 | + // error checking and 2^x checking removed for simplicity. |
| 61 | + long bits, val; |
| 62 | + do { |
| 63 | + bits = (ThreadLocalRandom.current().nextLong() << 1) >>> 1; |
| 64 | + val = bits % range_size; |
| 65 | + } |
| 66 | + while (bits - val + range_size < 0L); |
| 67 | + val += minimum; |
| 68 | + |
| 69 | + |
| 70 | + return val; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @param decimal_places |
| 75 | + * @param minimum |
| 76 | + * @param maximum |
| 77 | + * @return |
| 78 | + */ |
| 79 | + public double fixedPoint(int decimal_places, double minimum, double maximum) { |
| 80 | + |
| 81 | + |
| 82 | + int multiplier = 1; |
| 83 | + for (int i = 0; i < decimal_places; ++i) { |
| 84 | + multiplier *= 10; |
| 85 | + } |
| 86 | + |
| 87 | + int int_min = (int) (minimum * multiplier + 0.5); |
| 88 | + int int_max = (int) (maximum * multiplier + 0.5); |
| 89 | + |
| 90 | + return (double) this.number(int_min, int_max) / (double) multiplier; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @returns a random alphabetic string with length in range [minimum_length, maximum_length]. |
| 95 | + */ |
| 96 | + public String astring(int minimum_length, int maximum_length) { |
| 97 | + return randomString(minimum_length, maximum_length, 'a', 26); |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + /** |
| 102 | + * @returns a random numeric string with length in range [minimum_length, maximum_length]. |
| 103 | + */ |
| 104 | + public String nstring(int minimum_length, int maximum_length) { |
| 105 | + return randomString(minimum_length, maximum_length, '0', 10); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @param minimum_length |
| 110 | + * @param maximum_length |
| 111 | + * @param base |
| 112 | + * @param numCharacters |
| 113 | + * @return |
| 114 | + */ |
| 115 | + private String randomString(int minimum_length, int maximum_length, char base, int numCharacters) { |
| 116 | + int length = number(minimum_length, maximum_length); |
| 117 | + byte baseByte = (byte) base; |
| 118 | + byte[] bytes = new byte[length]; |
| 119 | + for (int i = 0; i < length; ++i) { |
| 120 | + bytes[i] = (byte) (baseByte + number(0, numCharacters - 1)); |
| 121 | + } |
| 122 | + return new String(bytes); |
| 123 | + } |
| 124 | + } |
0 commit comments