随机数

This commit is contained in:
Jesse-Ma
2022-12-26 11:07:24 +08:00
parent 3a2e7dc2d6
commit 3f92477c26
2 changed files with 13 additions and 5 deletions

View File

@@ -1 +1,3 @@
# flagnote-gateway
-Djava.security.egd=file:/dev/./urandom

View File

@@ -1,15 +1,21 @@
package com.flagnote.gateway.utils;
import java.util.concurrent.ThreadLocalRandom;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
public class RandomUtils {
public static SecureRandom sr = null;
public static ThreadLocalRandom getRandom() {
return ThreadLocalRandom.current();
static {
try {
sr = SecureRandom.getInstanceStrong();
} catch (NoSuchAlgorithmException e) {
sr = new SecureRandom();
}
}
public static Integer nextInt(Integer num) {
return getRandom().nextInt(num);
return sr.nextInt(num);
}
}