如何在 Java 中生成大随机数?
对于大随机数,在 Java 中使用 BigInteger 类型。首先,创建一个 Random 对象 -
Random randNum = new Random();
现在,声明一个字节数组并生成随机字节 -
byte[] b = new byte[max]; randNum.nextBytes(b);
现在,使用 BigInteger 类型生成一个大随机数 -
BigInteger bigInt = new BigInteger(b);
示例
import java.math.BigInteger;
import java.util.Random;
public class Demo {
public static void main(String... a) {
int max = 10;
Random randNum = new Random();
byte[] b = new byte[max];
randNum.nextBytes(b);
// BigInteger type
BigInteger bigInt = new BigInteger(b);
System.out.println("Very large random number = "+bigInt);
}
}输出
Very large random number = 283258678185163472552410
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP