-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomUtility.java
More file actions
62 lines (46 loc) · 2 KB
/
RandomUtility.java
File metadata and controls
62 lines (46 loc) · 2 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
package com.mrdheerajpurohit.utility;
import android.text.TextUtils;
import java.util.Random;
/**
* Dheeraj Purohit RandomUtility for Android.
*/
public final class RandomUtility {
public static final String NUMBERS = "0123456789";
public static final String LETTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static final String NUMBERS_AND_LETTERS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static final String CAPITAL_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static final String LOWER_CASE_LETTERS = "abcdefghijklmnopqrstuvwxyz";
public static String getRandomNumbersAndLetters(int length) {return getRandom(NUMBERS_AND_LETTERS, length);}
public static String getRandomNumbers(int length) {
return getRandom(NUMBERS, length);
}
public static String getRandomLetters(int length) {
return getRandom(LETTERS, length);
}
public static String getRandomCapitalLetters(int length) {return getRandom(CAPITAL_LETTERS, length);}
public static String getRandomLowerCaseLetters(int length) {return getRandom(LOWER_CASE_LETTERS, length);}
public static String getRandom(String source, int length) {return TextUtils.isEmpty(source) ? null : getRandom(source.toCharArray(), length);}
public static String getRandom(char[] sourceChar, int length)
{
if (sourceChar == null || sourceChar.length == 0 || length < 0)
{return null;}
StringBuilder str = new StringBuilder(length);
Random random = new Random();
for (int i = 0; i < length; i++) {
str.append(sourceChar[random.nextInt(sourceChar.length)]);
}
return str.toString();
}
public static int getRandom(int max) {
return getRandom(0, max);
}
public static int getRandom(int min, int max) {
if (min > max) {
return 0;
}
if (min == max) {
return min;
}
return min + new Random().nextInt(max - min);
}
}