Java 生成随机数
使用 SecureRandom
import java.security.SecureRandom;
public class Main {
public static void main(String[] args) {
SecureRandom secureRandom = new SecureRandom();
int randomInteger = secureRandom.nextInt(10);
System.out.println("Secure random Integer: " + randomInteger);
}
}使用 Apache Commons 的 UniformRandomProvider
在 Maven 中添加依赖:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-rng-simple</artifactId>
<version>1.3</version> <!-- Replace with the version you want -->
</dependency>然后:
import org.apache.commons.rng.simple.RandomSource;
import org.apache.commons.rng.UniformRandomProvider;
public class Main {
public static void main(String[] args) {
UniformRandomProvider rng = RandomSource.create(RandomSource.JDK);
int randomInteger2 = rng.nextInt(10);
System.out.println("Random Integer from UniformRandomProvider: " + randomInteger2);
}
}
评论已关闭