forked from examplehub/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomUtils.java
More file actions
64 lines (58 loc) · 1.81 KB
/
RandomUtils.java
File metadata and controls
64 lines (58 loc) · 1.81 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
package com.examplehub.utils;
import java.util.Random;
public class RandomUtils {
/**
* Generate random int numbers in a range.
*
* @param min the min value of random numbers.
* @param max the max value of random numbers.
* @param count the count of random numbers.
* @return {@code count} random numbers from {@code min} to {@code max} range.
*/
public static int[] randomInts(int min, int max, int count) {
if (min > max) {
throw new IllegalArgumentException("min value must be less than or equals to max");
}
int[] ints = new int[count];
Random random = new Random();
for (int i = 0; i < count; ++i) {
ints[i] = min + random.nextInt(max - min + 1);
}
return ints;
}
/**
* Generate random double numbers in a range.
*
* @param min the min value of random numbers.
* @param max the max value of random numbers.
* @param count the count of random numbers.
* @return {@code count} random numbers from {@code min} to {@code max} range.
*/
public static double[] randomDoubles(double min, double max, int count) {
if (min > max) {
throw new IllegalArgumentException("min value must be less than or equals to max");
}
double[] floats = new double[count];
Random random = new Random();
for (int i = 0; i < count; ++i) {
floats[i] = min + random.nextFloat() * (max - min);
}
return floats;
}
/**
* Generate random letters.
*
* @param count the count of random letters.
* @return {@code count} random letters.
*/
public static String randomLetters(int count) {
int min = 0;
int max = 127;
int[] chars = randomInts(min, max, count);
StringBuilder buffer = new StringBuilder();
for (int aChar : chars) {
buffer.append((char) aChar);
}
return buffer.toString();
}
}