java Java随机数生成器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12439942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java random number generator
提问by Ryan Sayles
Is it possible to have a user choose the number of digits of a random number, specifically a random Big Integer? For example if the user wants it to be 15 digits long the random number generator would only produce 15 digit long Big Integers.
是否可以让用户选择随机数的位数,特别是随机大整数?例如,如果用户希望它是 15 位长的随机数生成器将只产生 15 位长的大整数。
回答by dasblinkenlight
You can use the constructor of BigInteger
where you specify the number of binary digits: BigInteger(int numBits, Random rnd)
. You need roughly ten binary digits for each three decimal digits that the user wants. For example, if you need a 30-digit random BigInt
, use 100 binary digits.
您可以使用BigInteger
指定二进制位数的构造函数:BigInteger(int numBits, Random rnd)
. 对于用户想要的每三个十进制数字,您需要大约十个二进制数字。例如,如果您需要 30 位随机数BigInt
,请使用 100 位二进制数。
You can cut off the unnecessary digits by using remainder(10^30)
, and do it in a loop to ensure that the initial digit is not zero, ensuring the correct number of digits, like this:
您可以使用 截断不需要的数字remainder(10^30)
,并循环执行以确保初始数字不为零,确保正确的位数,如下所示:
Random rnd = new Random(123);
BigInteger tenPow30 = new BigInteger("10").pow(30);
BigInteger min = new BigInteger("10").pow(29);
BigInteger r;
do {
r = new BigInteger(100, rnd).remainder(tenPow30);
} while (r.compareTo(min) < 0);
System.out.println(r);
Link to a demo.
链接到演示。
回答by codaddict
You can always generate individual digits of the number randomly. This way for a 15 digit number you can generate 15 digits randomly and then form the number.
您始终可以随机生成数字的单个数字。这样对于 15 位数字,您可以随机生成 15 位数字,然后形成该数字。
Another way:
其他方式:
Lets change the problem to generate random 5 digit number.
让我们改变问题以生成随机的 5 位数字。
Min = 10000
Max = 99999
Now generate a random number between 0
and Max - Min
which is 0
and 89999
and add it to Min
.
现在在0
and之间生成一个随机数,Max - Min
它是0
and89999
并将其添加到Min
.
Random = Min + Math.random() * (Max - Min)
回答by Brian
Here are the steps:
以下是步骤:
- Generate n numbers
- Combine them using a
StringBuilder
- Create your number using
BigInteger(String)
- 生成 n 个数字
- 使用一个组合它们
StringBuilder
- 使用创建您的号码
BigInteger(String)
Here's some code:
这是一些代码:
public static BigInteger randomBigInt(int digits, Random rand) {
StringBuilder sb = new StringBuilder(digits);
// First digit can't be 0
sb.append(rand.nextInt(9) + 1);
int limit = digits - 1;
for (int i = 0; i < limit; i++)
sb.append(rand.nextInt(10));
return new BigInteger(sb.toString());
}
This generates each digit separately and adds them to a StringBuilder
(as opposed to an int
or something, which can generate buffer overflow issues) and then uses the resultant String
to create the BigInteger
. Also notice that the first digit will never be 0.
这会分别生成每个数字并将它们添加到 a StringBuilder
(而不是 anint
或其他东西,这会产生缓冲区溢出问题),然后使用结果String
创建BigInteger
. 还要注意第一个数字永远不会是 0。
回答by exp2Tapavicki
Using Random Generator like one here RandomUtil classyou can make random numbers between some values and much more.
使用 Random Generator 就像这里的 RandomUtil 类一样,您可以在某些值之间生成随机数等等。
For example using this code will be 15 digits long in range of min=100000000000000 max=999999999999999:
例如,使用此代码的长度为 15 位,范围为 min=100000000000000 max=999999999999999:
BigInteger number = RandomUtil.getRandomBigInteger(new BigInteger("100000000000000"), new BigInteger("999999999999999"), false);