-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRandomString.java
53 lines (45 loc) · 1.49 KB
/
RandomString.java
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
import java.util.Random;
// generate pesudorandom strings
class RandomString {
public static final int GENOME = 1;
public static final int LOWER = 2;
public static final int UPPER = 3;
private static char[] getAlphabets(int c) {
String s = "";
switch(c) {
case GENOME:
s = "ACTG";
break;
case UPPER:
s = "ABCDEFGHIJKLMNOPQRSUVWXYZ";
break;
case LOWER:
s = "abcdefghijklmnopqrstuvwxyz";
break;
default:
s = "Aa1";
break;
}
return s.toCharArray();
}
public static String nextString(int length, char[] alphabets) {
int upper = alphabets.length;
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; ++i) {
int index = random.nextInt(upper);
sb.append(alphabets[index]);
}
return sb.toString();
}
public static String nextString(int length, int choice) {
char[] array = getAlphabets(choice);
return nextString(length, array);
}
public static void main(String[] args) {
int length = 10;
System.out.println(RandomString.nextString(length, RandomString.GENOME));
System.out.println(RandomString.nextString(length, RandomString.UPPER));
System.out.println(RandomString.nextString(length, RandomString.LOWER));
}
}