-
Notifications
You must be signed in to change notification settings - Fork 863
Expand file tree
/
Copy pathRandomService.java
More file actions
114 lines (92 loc) · 3.07 KB
/
Copy pathRandomService.java
File metadata and controls
114 lines (92 loc) · 3.07 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
package com.github.javafaker.service;
import java.util.ArrayList;
import java.util.Random;
public class RandomService {
private static final Random SHARED_RANDOM = new Random();
private final Random random;
/**
* Uses a default shared random.
*/
public RandomService() {
this(SHARED_RANDOM);
}
/**
* @param random If null is passed in, a default Random is assigned
*/
public RandomService(Random random) {
this.random = random != null ? random : SHARED_RANDOM;
}
public int nextInt(int n) {
return random.nextInt(n);
}
public long nextLong() {
return random.nextLong();
}
// lifted from http://stackoverflow.com/questions/2546078/java-random-long-number-in-0-x-n-range
public long nextLong(long n) {
if (n <= 0) {
throw new IllegalArgumentException("bound must be positive");
}
long bits, val;
do {
long randomLong = random.nextLong();
bits = (randomLong << 1) >>> 1;
val = bits % n;
} while (bits - val + (n - 1) < 0L);
return val;
}
public double nextDouble() {
return random.nextDouble();
}
public Boolean nextBoolean() {
return random.nextBoolean();
}
public Integer nextInt(int min, int max) {
return random.nextInt((max - min) + 1) + min;
}
public String hex() {
return hex(8);
}
public String hex(int length) {
if (length <= 0) {
return ""; // Keep the existing behavior instead of throwing an error.
}
final char[] hexChars = new char[length];
for (int i = 0; i < length; i++) {
final int nextHex = nextInt(16);
if (nextHex < 10) {
hexChars[i] = (char) ('0' + nextHex);
} else {
hexChars[i] = (char) ('A' + nextHex - 10);
}
}
return new String(hexChars);
}
public String randomString(int length, char[] characters) {
if (length <= 0) {
return ""; // Keep the existing behavior instead of throwing an error.
}
StringBuilder randomString = new StringBuilder();
for (int i = 0; i < length; i++) {
char nextChar = characters[nextInt(characters.length)];
randomString.append(nextChar);
}
return randomString.toString();
}
public String randomString(int length) {
String digits = "0123456789";
String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String lower = upper.toLowerCase();
String alphanumeric = upper + lower + digits;
return randomString(length, alphanumeric.toCharArray());
}
public String randomString(int minLength, int maxLength) {
return randomString(nextInt(minLength, maxLength));
}
public String randomString(int minLength, int maxLength, char[] characters) {
return randomString(nextInt(minLength, maxLength), characters);
}
public String randomString() {
return randomString(nextInt(1, 255));
}
}