This question already has an answer here:
这个问题已经有了答案:
- How do I generate random integers within a specific range in Java? 57 answers
- 如何在Java的特定范围内生成随机整数?57个答案
I need to genarate a random number with exactly 6 digits in Java. I know i could loop 6 times over a randomicer but is there a nother way to do this in the standard Java SE ?
我需要在Java中使用精确的6位数的随机数字。我知道我可以在一个随机数上循环6次但是在标准的Java SE中有什么方法可以做到这一点吗?
EDIT: Follow up question: Now that I can generate my 6 digits i got a new problem, the whole ID I'm trying to create is of the syntax 123456-A1B45. So how do i randomice the last 5 chars that can be either A-Z or 0-9? I'm thinking of using the char value and randomice a number between 48 - 90 and simply drop any value that gets the numbers that represent 58-64. Is this the way to go or is there a better solution?
编辑:接下来的问题:现在我可以生成我的6位数字了,我有了一个新问题,我想要创建的整个ID是语法123456-A1B45。那么我如何随机化最后5个可以是A-Z或0-9的字符?我考虑的是在48 - 90之间使用char值和随机数,然后简单地删除任何表示58-64的值。这是去的路还是有更好的解决方案?
EDIT 2: This is my final solution. Thanks for all the help guys!
编辑2:这是我的最终解决方案。谢谢大家的帮助!
protected String createRandomRegistryId(String handleId)
{
// syntax we would like to generate is DIA123456-A1B34
String val = "DI";
// char (1), random A-Z
int ranChar = 65 + (new Random()).nextInt(90-65);
char ch = (char)ranChar;
val += ch;
// numbers (6), random 0-9
Random r = new Random();
int numbers = 100000 + (int)(r.nextFloat() * 899900);
val += String.valueOf(numbers);
val += "-";
// char or numbers (5), random 0-9 A-Z
for(int i = 0; i<6;){
int ranAny = 48 + (new Random()).nextInt(90-65);
if(!(57 < ranAny && ranAny<= 65)){
char c = (char)ranAny;
val += c;
i++;
}
}
return val;
}
8 个解决方案
#1
21
Generate a number in the range from 100000
to 999999
.
从100000到999999之间生成一个数字。
// pseudo code
int n = 100000 + random_float() * 900000;
I’m pretty sure you have already read the documentation for e.g. Random and can figure out the rest yourself.
我很确定你已经读过这个文档了,你可以自己弄清楚。
#2
50
To generate a 6-digit number:
生成6位数字:
Use Random
and nextInt
as follows:
使用随机和nextInt如下:
Random rnd = new Random();
int n = 100000 + rnd.nextInt(900000);
Note that n
will never be 7 digits (1000000) since nextInt(900000)
can at most return 899999
.
注意,自nextInt(900000)最多可以返回899999时,n永远不会是7位(1000000)。
So how do I randomize the last 5 chars that can be either A-Z or 0-9?
那么我如何随机化最后的5个字符可以是A-Z还是0-9?
Here's a simple solution:
这里有一个简单的解决方案:
// Generate random id, for example 283952-V8M32
char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
Random rnd = new Random();
StringBuilder sb = new StringBuilder((100000 + rnd.nextInt(900000)) + "-");
for (int i = 0; i < 5; i++)
sb.append(chars[rnd.nextInt(chars.length)]);
return sb.toString();
#3
5
If you need to specify the exact charactor length, we have to avoid values with 0 in-front.
如果您需要指定准确的角色长度,我们必须避免值为0的值。
Final String representation must have that exact character length.
最后的字符串表示必须具有精确的字符长度。
String GenerateRandomNumber(int charLength) {
return String.valueOf(charLength < 1 ? 0 : new Random()
.nextInt((9 * (int) Math.pow(10, charLength - 1)) - 1)
+ (int) Math.pow(10, charLength - 1));
}
#4
4
try this:
试试这个:
public int getRandomNumber(int min, int max) {
return (int) Math.floor(Math.random() * (max - min + 1)) + min;
}
#5
0
int rand = (new Random()).getNextInt(900000) + 100000;
EDIT: Fixed off-by-1 error and removed invalid solution.
编辑:修正错误并删除无效的解决方案。
#6
0
For the follow-up question, you can get a number between 36^5 and 36^6 and convert it in base 36
后续问题,你可以得到一个36 36 ^ ^ 5和6之间数量和转化基地36
UPDATED:
更新:
using this code
使用这个代码
http://javaconfessions.com/2008/09/convert-between-base-10-and-base-62-in_28.html
http://javaconfessions.com/2008/09/convert -基地- 10 -和-基础- 62 in_28.html
It's written BaseConverterUtil.toBase36(60466176+r.nextInt(2116316160))
这是写BaseConverterUtil.toBase36(60466176 + r.nextInt(2116316160))
but in your use case, it can be optimized by using a StringBuilder
and having the number in the reverse order ie 71 should be converted in Z1 instead of 1Z
但是在您的用例中,它可以通过使用StringBuilder来优化,并且在相反的顺序ie 71应该转换为Z1而不是1Z。
EDITED:
编辑:
#7
-1
Would that work for you?
这对你有用吗?
public class Main {
public static void main(String[] args) {
Random r = new Random(System.currentTimeMillis());
System.out.println(r.nextInt(100000) * 0.000001);
}
}
}
result e.g. 0.019007
结果如0.019007
#8
-2
Generate a random number (which is always between 0-1) and multiply by 1000000
生成一个随机数(它总是在0-1之间)并乘以1000000。
Math.round(Math.random()*1000000);
#1
21
Generate a number in the range from 100000
to 999999
.
从100000到999999之间生成一个数字。
// pseudo code
int n = 100000 + random_float() * 900000;
I’m pretty sure you have already read the documentation for e.g. Random and can figure out the rest yourself.
我很确定你已经读过这个文档了,你可以自己弄清楚。
#2
50
To generate a 6-digit number:
生成6位数字:
Use Random
and nextInt
as follows:
使用随机和nextInt如下:
Random rnd = new Random();
int n = 100000 + rnd.nextInt(900000);
Note that n
will never be 7 digits (1000000) since nextInt(900000)
can at most return 899999
.
注意,自nextInt(900000)最多可以返回899999时,n永远不会是7位(1000000)。
So how do I randomize the last 5 chars that can be either A-Z or 0-9?
那么我如何随机化最后的5个字符可以是A-Z还是0-9?
Here's a simple solution:
这里有一个简单的解决方案:
// Generate random id, for example 283952-V8M32
char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
Random rnd = new Random();
StringBuilder sb = new StringBuilder((100000 + rnd.nextInt(900000)) + "-");
for (int i = 0; i < 5; i++)
sb.append(chars[rnd.nextInt(chars.length)]);
return sb.toString();
#3
5
If you need to specify the exact charactor length, we have to avoid values with 0 in-front.
如果您需要指定准确的角色长度,我们必须避免值为0的值。
Final String representation must have that exact character length.
最后的字符串表示必须具有精确的字符长度。
String GenerateRandomNumber(int charLength) {
return String.valueOf(charLength < 1 ? 0 : new Random()
.nextInt((9 * (int) Math.pow(10, charLength - 1)) - 1)
+ (int) Math.pow(10, charLength - 1));
}
#4
4
try this:
试试这个:
public int getRandomNumber(int min, int max) {
return (int) Math.floor(Math.random() * (max - min + 1)) + min;
}
#5
0
int rand = (new Random()).getNextInt(900000) + 100000;
EDIT: Fixed off-by-1 error and removed invalid solution.
编辑:修正错误并删除无效的解决方案。
#6
0
For the follow-up question, you can get a number between 36^5 and 36^6 and convert it in base 36
后续问题,你可以得到一个36 36 ^ ^ 5和6之间数量和转化基地36
UPDATED:
更新:
using this code
使用这个代码
http://javaconfessions.com/2008/09/convert-between-base-10-and-base-62-in_28.html
http://javaconfessions.com/2008/09/convert -基地- 10 -和-基础- 62 in_28.html
It's written BaseConverterUtil.toBase36(60466176+r.nextInt(2116316160))
这是写BaseConverterUtil.toBase36(60466176 + r.nextInt(2116316160))
but in your use case, it can be optimized by using a StringBuilder
and having the number in the reverse order ie 71 should be converted in Z1 instead of 1Z
但是在您的用例中,它可以通过使用StringBuilder来优化,并且在相反的顺序ie 71应该转换为Z1而不是1Z。
EDITED:
编辑:
#7
-1
Would that work for you?
这对你有用吗?
public class Main {
public static void main(String[] args) {
Random r = new Random(System.currentTimeMillis());
System.out.println(r.nextInt(100000) * 0.000001);
}
}
}
result e.g. 0.019007
结果如0.019007
#8
-2
Generate a random number (which is always between 0-1) and multiply by 1000000
生成一个随机数(它总是在0-1之间)并乘以1000000。
Math.round(Math.random()*1000000);