|
| 1 | +package tools.dynamia.commons.math; |
| 2 | + |
| 3 | +import java.time.LocalDate; |
| 4 | +import java.time.LocalDateTime; |
| 5 | +import java.time.LocalTime; |
| 6 | +import java.util.concurrent.ThreadLocalRandom; |
| 7 | + |
| 8 | +/** |
| 9 | + * Utility class for generating random numbers and strings. |
| 10 | + * Uses {@link ThreadLocalRandom} for better performance in concurrent environments. |
| 11 | + */ |
| 12 | +public class Randoms { |
| 13 | + |
| 14 | + /** |
| 15 | + * Returns a random integer between the specified origin (inclusive) and the specified bound (exclusive). |
| 16 | + * |
| 17 | + * @param min the least value returned |
| 18 | + * @param max the upper bound (exclusive) |
| 19 | + * @return a random integer between {@code min} (inclusive) and {@code max} (exclusive) |
| 20 | + */ |
| 21 | + public static int nextInt(int min, int max) { |
| 22 | + return ThreadLocalRandom.current().nextInt(min, max); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Returns a random integer between 0 (inclusive) and the specified bound (exclusive). |
| 27 | + * |
| 28 | + * @param max the upper bound (exclusive) |
| 29 | + * @return a random integer between 0 (inclusive) and {@code max} (exclusive) |
| 30 | + */ |
| 31 | + public static int nextInt(int max) { |
| 32 | + return ThreadLocalRandom.current().nextInt(max); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Returns a random integer. |
| 37 | + * |
| 38 | + * @return a random integer |
| 39 | + */ |
| 40 | + public static int nextInt() { |
| 41 | + return ThreadLocalRandom.current().nextInt(0, Integer.MAX_VALUE); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Returns a random long between the specified origin (inclusive) and the specified bound (exclusive). |
| 46 | + * |
| 47 | + * @param min the least value returned |
| 48 | + * @param max the upper bound (exclusive) |
| 49 | + * @return a random long between {@code min} (inclusive) and {@code max} (exclusive) |
| 50 | + */ |
| 51 | + public static long nextLong(long min, long max) { |
| 52 | + return ThreadLocalRandom.current().nextLong(min, max); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Returns a random float between the specified origin (inclusive) and the specified bound (exclusive). |
| 57 | + * |
| 58 | + * @param min the least value returned |
| 59 | + * @param max the upper bound (exclusive) |
| 60 | + * @return a random float between {@code min} (inclusive) and {@code max} (exclusive) |
| 61 | + */ |
| 62 | + public static float nextFloat(float min, float max) { |
| 63 | + return min + ThreadLocalRandom.current().nextFloat() * (max - min); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Returns a random double between the specified origin (inclusive) and the specified bound (exclusive). |
| 68 | + * |
| 69 | + * @param min the least value returned |
| 70 | + * @param max the upper bound (exclusive) |
| 71 | + * @return a random double between {@code min} (inclusive) and {@code max} (exclusive) |
| 72 | + */ |
| 73 | + public static double nextDouble(double min, double max) { |
| 74 | + return ThreadLocalRandom.current().nextDouble(min, max); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Generates a random string of the specified length using alphanumeric characters. |
| 79 | + * |
| 80 | + * @param length the length of the random string |
| 81 | + * @return a random string of alphanumeric characters |
| 82 | + */ |
| 83 | + public static String nextString(int length) { |
| 84 | + StringBuilder sb = new StringBuilder(); |
| 85 | + String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; |
| 86 | + for (int i = 0; i < length; i++) { |
| 87 | + int index = nextInt(chars.length()); |
| 88 | + sb.append(chars.charAt(index)); |
| 89 | + } |
| 90 | + return sb.toString(); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Generates a random string of the specified length using numeric characters. |
| 95 | + * |
| 96 | + * @param length the length of the random string |
| 97 | + * @return a random string of numeric characters |
| 98 | + */ |
| 99 | + public static String nextNumericString(int length) { |
| 100 | + StringBuilder sb = new StringBuilder(); |
| 101 | + String chars = "0123456789"; |
| 102 | + for (int i = 0; i < length; i++) { |
| 103 | + int index = nextInt(chars.length()); |
| 104 | + sb.append(chars.charAt(index)); |
| 105 | + } |
| 106 | + return sb.toString(); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Generates a random string of the specified length using alphabetic characters. |
| 111 | + * |
| 112 | + * @param length the length of the random string |
| 113 | + * @return a random string of alphabetic characters |
| 114 | + */ |
| 115 | + public static String nextAlphabeticString(int length) { |
| 116 | + StringBuilder sb = new StringBuilder(); |
| 117 | + String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
| 118 | + for (int i = 0; i < length; i++) { |
| 119 | + int index = nextInt(chars.length()); |
| 120 | + sb.append(chars.charAt(index)); |
| 121 | + } |
| 122 | + return sb.toString(); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Generates a random string of the specified length using hexadecimal characters. |
| 127 | + * |
| 128 | + * @param length the length of the random string |
| 129 | + * @return a random string of hexadecimal characters |
| 130 | + */ |
| 131 | + public static String nextHexString(int length) { |
| 132 | + StringBuilder sb = new StringBuilder(); |
| 133 | + String chars = "0123456789ABCDEF"; |
| 134 | + for (int i = 0; i < length; i++) { |
| 135 | + int index = nextInt(chars.length()); |
| 136 | + sb.append(chars.charAt(index)); |
| 137 | + } |
| 138 | + return sb.toString(); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Generates a random hex color string (e.g., #FF0000). |
| 143 | + * |
| 144 | + * @return a random hex color string |
| 145 | + */ |
| 146 | + public static String nextHexColor() { |
| 147 | + return "#" + nextHexString(6); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Generates a random UUID string. |
| 152 | + * |
| 153 | + * @return a random UUID string |
| 154 | + */ |
| 155 | + public static String nextUUID() { |
| 156 | + return java.util.UUID.randomUUID().toString(); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Returns a random name from a predefined list of names. |
| 161 | + * |
| 162 | + * @return a random name |
| 163 | + */ |
| 164 | + public static String nextName() { |
| 165 | + String[] names = {"Alice", "Bob", "Charlie", "David", "Eve", "Frank", "Grace", "Hannah", "Ivy", "Jack", |
| 166 | + "Kathy", "Liam", "Mia", "Noah", "Olivia", "Paul", "Quinn", "Rachel", "Sam", "Tina", |
| 167 | + "Uma", "Victor", "Wendy", "Xander", "Yara", "Zane", "Aaron", "Bella", "Carter", "Diana", "Ethan", "Fiona", |
| 168 | + "Gavin", "Hailey", "Ian", "Jasmine", "Kevin", "Luna", "Mason", "Nora", "Owen", "Piper", "Quincy", "Ruby", "Sean", "Tara", |
| 169 | + "Mario", "Nina", "Leo", "Eva", "Jake", "Lily", "Cindy", "Derek", "Elena", "Felix", "Gloria"}; |
| 170 | + return names[nextInt(names.length)]; |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Returns a random element from the specified array. |
| 175 | + * |
| 176 | + * @param <T> the type of the elements |
| 177 | + * @param array the array to choose from |
| 178 | + * @return a random element from the array, or {@code null} if the array is null or empty |
| 179 | + */ |
| 180 | + public static <T> T chooseRandom(T[] array) { |
| 181 | + if (array == null || array.length == 0) { |
| 182 | + return null; |
| 183 | + } |
| 184 | + int index = nextInt(array.length); |
| 185 | + return array[index]; |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Returns a random element from the specified array, or the default value if the array is null or empty. |
| 190 | + * |
| 191 | + * @param <T> the type of the elements |
| 192 | + * @param array the array to choose from |
| 193 | + * @param defaultValue the value to return if the array is null or empty |
| 194 | + * @return a random element from the array, or {@code defaultValue} |
| 195 | + */ |
| 196 | + public static <T> T chooseRandom(T[] array, T defaultValue) { |
| 197 | + if (array == null || array.length == 0) { |
| 198 | + return defaultValue; |
| 199 | + } |
| 200 | + |
| 201 | + int index = nextInt(array.length); |
| 202 | + return array[index]; |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Returns a random element from the specified list. |
| 207 | + * |
| 208 | + * @param <T> the type of the elements |
| 209 | + * @param list the list to choose from |
| 210 | + * @return a random element from the list, or {@code null} if the list is null or empty |
| 211 | + */ |
| 212 | + public static <T> T chooseRandom(java.util.List<T> list) { |
| 213 | + if (list == null || list.isEmpty()) { |
| 214 | + return null; |
| 215 | + } |
| 216 | + int index = nextInt(list.size()); |
| 217 | + return list.get(index); |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + * Returns a random date between the specified start (inclusive) and end (exclusive) dates. |
| 222 | + * |
| 223 | + * @param startInclusive the start date (inclusive) |
| 224 | + * @param endExclusive the end date (exclusive) |
| 225 | + * @return a random date between {@code startInclusive} and {@code endExclusive} |
| 226 | + */ |
| 227 | + public static LocalDate nextDate(LocalDate startInclusive, LocalDate endExclusive) { |
| 228 | + long startEpochDay = startInclusive.toEpochDay(); |
| 229 | + long endEpochDay = endExclusive.toEpochDay(); |
| 230 | + long randomDay = nextLong(startEpochDay, endEpochDay); |
| 231 | + return LocalDate.ofEpochDay(randomDay); |
| 232 | + } |
| 233 | + |
| 234 | + /** |
| 235 | + * Returns a random date in the past, up to the specified maximum number of days. |
| 236 | + * |
| 237 | + * @param maxDaysInPast the maximum number of days in the past |
| 238 | + * @return a random date in the past |
| 239 | + */ |
| 240 | + public static LocalDate nextPastDate(int maxDaysInPast) { |
| 241 | + LocalDate today = LocalDate.now(); |
| 242 | + long randomDays = nextLong(1, maxDaysInPast + 1); |
| 243 | + return today.minusDays(randomDays); |
| 244 | + } |
| 245 | + |
| 246 | + /** |
| 247 | + * Returns a random date in the future, up to the specified maximum number of days. |
| 248 | + * |
| 249 | + * @param maxDaysInFuture the maximum number of days in the future |
| 250 | + * @return a random date in the future |
| 251 | + */ |
| 252 | + public static LocalDate nextFutureDate(int maxDaysInFuture) { |
| 253 | + LocalDate today = LocalDate.now(); |
| 254 | + long randomDays = nextLong(1, maxDaysInFuture + 1); |
| 255 | + return today.plusDays(randomDays); |
| 256 | + } |
| 257 | + |
| 258 | + /** |
| 259 | + * Returns a random date-time between the specified start (inclusive) and end (exclusive) date-times. |
| 260 | + * |
| 261 | + * @param startInclusive the start date-time (inclusive) |
| 262 | + * @param endExclusive the end date-time (exclusive) |
| 263 | + * @return a random date-time between {@code startInclusive} and {@code endExclusive} |
| 264 | + */ |
| 265 | + public static LocalDateTime nextDateTime(LocalDateTime startInclusive, LocalDateTime endExclusive) { |
| 266 | + long startEpochSecond = startInclusive.toEpochSecond(java.time.ZoneOffset.UTC); |
| 267 | + long endEpochSecond = endExclusive.toEpochSecond(java.time.ZoneOffset.UTC); |
| 268 | + long randomSecond = nextLong(startEpochSecond, endEpochSecond); |
| 269 | + return LocalDateTime.ofEpochSecond(randomSecond, 0, java.time.ZoneOffset.UTC); |
| 270 | + } |
| 271 | + |
| 272 | + /** |
| 273 | + * Returns a random time of day. |
| 274 | + * |
| 275 | + * @return a random time |
| 276 | + */ |
| 277 | + public static LocalTime nextTime() { |
| 278 | + int hour = nextInt(0, 24); |
| 279 | + int minute = nextInt(0, 60); |
| 280 | + int second = nextInt(0, 60); |
| 281 | + return LocalTime.of(hour, minute, second); |
| 282 | + } |
| 283 | + |
| 284 | + /** |
| 285 | + * Returns a random time between the specified minimum hour (inclusive) and maximum hour (exclusive). |
| 286 | + * |
| 287 | + * @param minHour the minimum hour (inclusive) |
| 288 | + * @param maxHour the maximum hour (exclusive) |
| 289 | + * @return a random time between {@code minHour} and {@code maxHour} |
| 290 | + */ |
| 291 | + public static LocalTime nextTime(int minHour, int maxHour) { |
| 292 | + int hour = nextInt(minHour, maxHour); |
| 293 | + int minute = nextInt(0, 60); |
| 294 | + int second = nextInt(0, 60); |
| 295 | + return LocalTime.of(hour, minute, second); |
| 296 | + } |
| 297 | +} |
0 commit comments