[英]Generating 10 digits unique random number in java
I am trying with below code to generate 10 digits unique random number.我正在尝试使用以下代码生成 10 位唯一随机数。 As per my req i have to create around 5000 unique numbers(ids).
根据我的要求,我必须创建大约 5000 个唯一数字(id)。 This is not working as expected.
这没有按预期工作。 It also generates -ve numbers.
它还生成 -ve 数字。 Also sometimes one or two digits are missing in generated number resulting in 8 or 9 numbers not 10.
此外,有时生成的数字中缺少一位或两位数字,导致 8 或 9 个数字而不是 10。
public static synchronized List generateRandomPin(){
int START =1000000000;
//int END = Integer.parseInt("9999999999");
//long END = Integer.parseInt("9999999999");
long END = 9999999999L;
Random random = new Random();
for (int idx = 1; idx <= 3000; ++idx){
createRandomInteger(START, END, random);
}
return null;
}
private static void createRandomInteger(int aStart, long aEnd, Random aRandom){
if ( aStart > aEnd ) {
throw new IllegalArgumentException("Start cannot exceed End.");
}
//get the range, casting to long to avoid overflow problems
long range = (long)aEnd - (long)aStart + 1;
logger.info("range>>>>>>>>>>>"+range);
// compute a fraction of the range, 0 <= frac < range
long fraction = (long)(range * aRandom.nextDouble());
logger.info("fraction>>>>>>>>>>>>>>>>>>>>"+fraction);
int randomNumber = (int)(fraction + aStart);
logger.info("Generated : " + randomNumber);
}
So you want a fixed length random number of 10 digits?所以你想要一个固定长度的 10 位随机数? This can be done easier:
这可以更容易地完成:
long number = (long) Math.floor(Math.random() * 9_000_000_000L) + 1_000_000_000L;
Note that 10-digit numbers over Integer.MAX_VALUE
doesn't fit in an int
, hence the long
.请注意,
Integer.MAX_VALUE
上的 10 位数字不适合int
,因此long
。
I think the reason you're getting 8/9 digit values and negative numbers is that you're adding fraction
, a long
(signed 64-bit value) which may be larger than the positive int
range (32-bit value) to aStart
.我认为你得到8/9位值和负数的原因是,你要添加
fraction
,一个long
(符号的64位值),这可能比正面大int
到范围(32位值) aStart
.
The value is overflowing such that randomNumber
is in the negative 32-bit range or has almost wrapped around to aStart
(since int
is a signed 32-bit value, fraction
would only need to be slightly less than (2^32 - aStart
) for you to see 8 or 9 digit values).该值正在溢出,以至于
randomNumber
处于负 32 位范围内或几乎环绕到aStart
(因为int
是有符号的 32 位值,因此fraction
只需稍小于 (2^32 - aStart
) 即可您可以看到 8 位或 9 位数字值)。
You need to use long
for all the values.您需要对所有值使用
long
。
private static void createRandomInteger(int aStart, long aEnd, Random aRandom){
if ( aStart > aEnd ) {
throw new IllegalArgumentException("Start cannot exceed End.");
}
//get the range, casting to long to avoid overflow problems
long range = aEnd - (long)aStart + 1;
logger.info("range>>>>>>>>>>>"+range);
// compute a fraction of the range, 0 <= frac < range
long fraction = (long)(range * aRandom.nextDouble());
logger.info("fraction>>>>>>>>>>>>>>>>>>>>"+fraction);
long randomNumber = fraction + (long)aStart;
logger.info("Generated : " + randomNumber);
}
I don't know why noone realized that but I think the point is to generate "unique" random number which I am also trying to do that.我不知道为什么没有人意识到这一点,但我认为重点是生成“唯一”随机数,我也在尝试这样做。 I managed to generate 11 digits random number but I am not sure how to generate unique numbers.
我设法生成了 11 位随机数,但我不确定如何生成唯一数字。 Also my approach is a little different.
我的方法也有点不同。 In this method I am appending number chars next to each other with for loop.
在这种方法中,我使用 for 循环在彼此旁边附加数字字符。 Then returning long number.
然后返回长号。
public long generateID() {
Random rnd = new Random();
char [] digits = new char[11];
digits[0] = (char) (rnd.nextInt(9) + '1');
for(int i=1; i<digits.length; i++) {
digits[i] = (char) (rnd.nextInt(10) + '0');
}
return Long.parseLong(new String(digits));
}
我会用
long theRandomNum = (long) (Math.random()*Math.pow(10,10));
This is a utility method for generating a fixed length random number.这是一种生成固定长度随机数的实用方法。
public final static String createRandomNumber(long len) {
if (len > 18)
throw new IllegalStateException("To many digits");
long tLen = (long) Math.pow(10, len - 1) * 9;
long number = (long) (Math.random() * tLen) + (long) Math.pow(10, len - 1) * 1;
String tVal = number + "";
if (tVal.length() != len) {
throw new IllegalStateException("The random number '" + tVal + "' is not '" + len + "' digits");
}
return tVal;
}
A general solution to return a 'n' digit number is返回“n”位数字的一般解决方案是
Math.floor(Math.random() * (9*Math.pow(10,n-1))) + Math.pow(10,(n-1))
For n=3, This would return numbers from 100 to 999 and so on.对于 n=3,这将返回从 100 到 999 的数字,依此类推。
You can even control the end range, ie from 100 to 5xx but setting the "9" in the above equation "5" or any other number from 1-9您甚至可以控制结束范围,即从 100 到 5xx 但在上面的等式中设置“9”“5”或任何其他数字 1-9
Hi you can use the following method to generate 10 digit random number嗨,您可以使用以下方法生成 10 位随机数
private static int getRndNumber() {
Random random=new Random();
int randomNumber=0;
boolean loop=true;
while(loop) {
randomNumber=random.nextInt();
if(Integer.toString(randomNumber).length()==10 && !Integer.toString(randomNumber).startsWith("-")) {
loop=false;
}
}
return randomNumber;
}
Maybe you are looking for this one:也许你正在寻找这个:
Random rand = new Random();
long drand = (long)(rand.nextDouble()*10000000000L);
You can simply put this inside a loop.你可以简单地把它放在一个循环中。
this is for random number starting from 1 and 2 (10 digits).这是从 1 和 2(10 位数字)开始的随机数。
public int gen() {
Random r = new Random(System.currentTimeMillis());
return 1000000000 + r.nextInt(2000000000);
}
hopefully it works.希望它有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.