java 如何创建一个随机的 16 位数字,具有特定的第一位数字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27843947/
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
How to create a random 16 digits number, with specific first digits?
提问by Marios Ath
I want to create a randomly generated 16 digit-number in java.But there is a catch I need the first two digits to be "52". For example, 5289-7894-2435-1967. I was thinking of using a random generator and create a 14 digit number and then add an integer 5200 0000 0000 0000. I tried looking for similar problems and can't really find something useful. I'm not familiar with the math method,maybe it could solve the problem for me.
我想在java中创建一个随机生成的16位数字。但是有一个问题,我需要前两位数字为“52”。例如,5289-7894-2435-1967。我正在考虑使用随机生成器并创建一个 14 位数字,然后添加一个整数 5200 0000 0000 0000。我尝试寻找类似的问题,但找不到真正有用的东西。我对数学方法不熟悉,也许它可以为我解决问题。
采纳答案by Fund Monica's Lawsuit
First, you need to generate a random 14-digit number, like you've done:
首先,您需要生成一个随机的 14 位数字,就像您所做的那样:
long first14 = (long) (Math.random() * 100000000000000L);
Then you add the 52
at the beginning.
然后52
在开头添加。
long number = 5200000000000000L + first14;
One other way that would work equally well, and will save memory, since Math.random()
creates an internal Random
object:
另一种同样有效并节省内存的方法,因为Math.random()
创建了一个内部Random
对象:
//Declare this before you need to use it
java.util.Random rng = new java.util.Random(); //Provide a seed if you want the same ones every time
...
//Then, when you need a number:
long first14 = (rng.nextLong() % 100000000000000L) + 5200000000000000L;
//Or, to mimic the Math.random() option
long first14 = (rng.nextDouble() * 100000000000000L) + 5200000000000000L;
Note that nextLong() % n
will not provide a perfectly random distribution, unlike Math.random()
. However, if you're just generating test data and it doesn't have to be cryptographically secure, it works as well. It's up to you which one to use.
请注意nextLong() % n
,与 不同,它不会提供完美的随机分布Math.random()
。但是,如果您只是生成测试数据并且它不必是加密安全的,那么它也能正常工作。使用哪一种取决于您。
回答by weston
Random rand = new Random();
String yourValue = String.format((Locale)null, //don't want any thousand separators
"52%02d-%04d-%04d-%04d",
rand.nextInt(100),
rand.nextInt(10000),
rand.nextInt(10000),
rand.nextInt(10000));
回答by sol4me
You can generate 14 random digits and then append at the beginning "52". E.g.
您可以生成 14 个随机数字,然后在开头附加“52”。例如
public class Tes {
public static void main(String[] args) {
System.out.println(generateRandom(52));
}
public static long generateRandom(int prefix) {
Random rand = new Random();
long x = (long)(rand.nextDouble()*100000000000000L);
String s = String.valueOf(prefix) + String.format("%014d", x);
return Long.valueOf(s);
}
}
回答by Jordi Castilla
- Create a 14 random digit number. with
Math.random
- Concatenate to it at the begining the "52"
String
. - Convert the string with
Integer.parseInt(String)
method
- 创建一个 14 位随机数字。和
Math.random
- 在 "52" 开头连接它
String
。 - 用
Integer.parseInt(String)
方法转换字符串