随机数

This commit is contained in:
Jesse-Ma
2022-12-26 11:07:06 +08:00
parent dce7172987
commit a82eef1afe
2 changed files with 12 additions and 4 deletions

View File

@@ -1,15 +1,21 @@
package com.flagnote.note.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);
}
}