2022-07-20 13:18:57 +02:00
|
|
|
package fr.pandacube.lib.util;
|
2016-02-16 20:07:51 +01:00
|
|
|
|
2018-07-21 17:57:44 +02:00
|
|
|
import java.util.List;
|
2016-02-16 20:07:51 +01:00
|
|
|
import java.util.Random;
|
2019-03-15 19:01:34 +01:00
|
|
|
import java.util.Set;
|
2016-02-16 20:07:51 +01:00
|
|
|
|
|
|
|
public class RandomUtil {
|
2016-07-14 14:22:23 +02:00
|
|
|
|
2022-07-10 00:55:56 +02:00
|
|
|
public static final Random rand = new Random();
|
2016-07-14 14:22:23 +02:00
|
|
|
|
2016-02-16 20:07:51 +01:00
|
|
|
public static int nextIntBetween(int minInclu, int maxExclu) {
|
2016-07-14 14:22:23 +02:00
|
|
|
return rand.nextInt(maxExclu - minInclu) + minInclu;
|
2016-02-16 20:07:51 +01:00
|
|
|
}
|
2016-07-14 14:22:23 +02:00
|
|
|
|
2016-02-16 20:07:51 +01:00
|
|
|
public static double nextDoubleBetween(double minInclu, double maxExclu) {
|
2016-07-14 14:22:23 +02:00
|
|
|
return rand.nextDouble() * (maxExclu - minInclu) + minInclu;
|
2016-02-16 20:07:51 +01:00
|
|
|
}
|
2018-07-21 17:57:44 +02:00
|
|
|
|
|
|
|
public static <T> T arrayElement(T[] arr) {
|
2020-11-02 23:23:41 +01:00
|
|
|
return (arr == null || arr.length == 0) ? null : arr[rand.nextInt(arr.length)];
|
2018-07-21 17:57:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static <T> T listElement(List<T> arr) {
|
2020-11-02 23:23:41 +01:00
|
|
|
return (arr == null || arr.isEmpty()) ? null : arr.get(rand.nextInt(arr.size()));
|
2018-07-21 17:57:44 +02:00
|
|
|
}
|
2019-03-15 19:01:34 +01:00
|
|
|
|
2022-01-23 16:17:44 +01:00
|
|
|
public static char stringChar(String arr) {
|
|
|
|
return (arr == null || arr.isEmpty()) ? '\0' : arr.charAt(rand.nextInt(arr.length()));
|
|
|
|
}
|
|
|
|
|
2019-03-15 19:01:34 +01:00
|
|
|
/**
|
|
|
|
* Returns a random value from a set.
|
|
|
|
*
|
|
|
|
* May not be optimized (Actually O(n) )
|
2022-07-10 00:55:56 +02:00
|
|
|
* @param set the Set from which to pick a random value
|
|
|
|
* @return a random value from the set
|
2019-03-15 19:01:34 +01:00
|
|
|
*/
|
|
|
|
public static <T> T setElement(Set<T> set) {
|
|
|
|
if (set.isEmpty())
|
|
|
|
throw new IllegalArgumentException("set is empty");
|
|
|
|
int retI = rand.nextInt(set.size()), i = 0;
|
|
|
|
for (T e : set) {
|
|
|
|
if (retI == i)
|
|
|
|
return e;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
throw new RuntimeException("Should never go to this line of code");
|
|
|
|
}
|
2021-08-23 02:24:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a value between 0 and the number of parameter minus 1, using the provided frequencies.
|
|
|
|
*
|
|
|
|
* The probability of each value to be returned depends of the frequencies provided.
|
2022-07-10 00:55:56 +02:00
|
|
|
* @param f the frequencies of each entries
|
2021-08-23 02:24:34 +02:00
|
|
|
* @return the index of an entry, or -1 if it is unable to pick anything (all the frequencies are 0 or there is not provided frequency)
|
|
|
|
*/
|
2022-07-10 00:55:56 +02:00
|
|
|
public static int randomIndexOfFrequencies(double... f) {
|
|
|
|
if (f == null)
|
|
|
|
throw new IllegalArgumentException("f cannot be null");
|
|
|
|
int n = f.length;
|
|
|
|
double[] fSums = new double[n];
|
2021-08-23 02:24:34 +02:00
|
|
|
double sum = 0;
|
2022-07-10 00:55:56 +02:00
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
if (f[i] < 0)
|
|
|
|
throw new IllegalArgumentException("f[" + i + "] cannot be negative.");
|
|
|
|
fSums[i] = (sum += f[i]);
|
|
|
|
}
|
2021-08-23 02:24:34 +02:00
|
|
|
double r = rand.nextDouble() * sum;
|
2022-07-10 00:55:56 +02:00
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
if (fSums[i] > r)
|
2021-08-23 02:24:34 +02:00
|
|
|
return i;
|
|
|
|
}
|
2022-07-10 00:55:56 +02:00
|
|
|
return n - 1;
|
2021-08-23 02:24:34 +02:00
|
|
|
}
|
2022-01-23 16:17:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static final String PASSWORD_CHARSET_LATIN_LOWERCASE = "abcdefghijklmnopqrstuvwxyz";
|
|
|
|
public static final String PASSWORD_CHARSET_LATIN_UPPERCASE = PASSWORD_CHARSET_LATIN_LOWERCASE.toUpperCase();
|
|
|
|
public static final String PASSWORD_CHARSET_DIGIT = "0123456789";
|
2022-07-10 00:55:56 +02:00
|
|
|
public static final String PASSWORD_CHARSET_SPECIAL = "@#+*/-;:,.?!='()[]{}&";
|
2022-01-23 16:17:44 +01:00
|
|
|
public static final String PASSWORD_CHARSET_NO_ANBIGUITY = "abcdefghkmnpqrstwxyzACDEFGHKLMNPQRSTWXYZ2345679";
|
|
|
|
|
|
|
|
public static String randomPassword(int length) {
|
|
|
|
return randomPassword(length, PASSWORD_CHARSET_NO_ANBIGUITY);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String randomPassword(int length, String charset) {
|
|
|
|
char[] pw = new char[length];
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
pw[i] = stringChar(charset);
|
|
|
|
}
|
|
|
|
return String.valueOf(pw);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-16 20:07:51 +01:00
|
|
|
|
|
|
|
}
|