-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomAlphaNumeric.java
36 lines (26 loc) · 1.04 KB
/
RandomAlphaNumeric.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
package id.ac.its.kij.simplechat;
import java.security.SecureRandom;
import java.util.Random;
public class RandomAlphaNumeric {
// inisialisasi set karakter (alpha-numeric)
private static final char[] CHARSET_azAZ09 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
public static String randomString(char[] characterSet, int length) {
// secure random dan simpan hasil sesuai parameter panjang
Random random = new SecureRandom();
char[] result = new char[length];
// melakukan random karakter set yang telah didefinisikan
for (int i = 0; i < result.length; i++) {
// ambil index random dari parameter panjang
// jadikan index tersebut sebagai karakter yang diambil
int randomCharIdx = random.nextInt(characterSet.length);
result[i] = characterSet[randomCharIdx];
}
return new String(result);
}
public static void main(String[] args) {
// uji hasil dengan jumlah 10 karakter
for (int i = 0; i < 10; i++) {
System.out.println(randomString(CHARSET_azAZ09, 10));
}
}
}