Skip to content

Commit 6f7833c

Browse files
committed
add getRandom(int max)
1 parent eb79e22 commit 6f7833c

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/cn/trinea/android/common/util/RandomUtils.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,40 @@ public static String getRandom(char[] sourceChar, int length) {
106106
}
107107
return str.toString();
108108
}
109+
110+
/**
111+
* get random int between 0 and max
112+
*
113+
* @param max
114+
* @return <ul>
115+
* <li>if max <= 0, return 0</li>
116+
* <li>else return random int between 0 and max</li>
117+
* </ul>
118+
*/
119+
public static int getRandom(int max) {
120+
return getRandom(0, max);
121+
}
122+
123+
/**
124+
* get random int between min and max
125+
*
126+
* @param min
127+
* @param max
128+
* @return <ul>
129+
* <li>if min > max, return 0</li>
130+
* <li>if min = max, return min</li>
131+
* <li>else return random int between min and max</li>
132+
* </ul>
133+
*/
134+
public static int getRandom(int min, int max) {
135+
if (min > max) {
136+
return 0;
137+
}
138+
if (min == max) {
139+
return min;
140+
}
141+
142+
Random random = new Random();
143+
return min + random.nextInt(max - min);
144+
}
109145
}

0 commit comments

Comments
 (0)