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
46 lines (42 loc) · 1.39 KB
/
RandomUtils.java
File metadata and controls
46 lines (42 loc) · 1.39 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
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] = random.nextInt(max - min + 1) + min;
}
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;
}
}