Java 使用SecureRandom生成安全随机数

Java 使用SecureRandom生成安全随机数,java,secure-random,Java,Secure Random,如何使用Java的SecureRandom类生成一个6位整数 我正在尝试使用以下代码生成随机数: SecureRandom secureRandom = new SecureRandom(); int secureNumber = secureRandom.nextInt(); 它生成任意长度的随机数,包括负数。我在SecureRandom类中找不到提供一系列数字的方法。我想生成一个6位数的正随机数,只需使用数组即可 int[] arr = new int[6]; Random rand =

如何使用Java的
SecureRandom
类生成一个6位整数

我正在尝试使用以下代码生成随机数:

SecureRandom secureRandom = new SecureRandom();
int secureNumber = secureRandom.nextInt();

它生成任意长度的随机数,包括负数。我在
SecureRandom
类中找不到提供一系列数字的方法。我想生成一个6位数的正随机数,只需使用数组即可

int[] arr = new int[6];
Random rand = new SecureRandom();
for(int i= 0; i< 6 ; i++){
    // 0 to 9
    arr[i] = rand.nextInt(10);   
}
int[]arr=newint[6];
Random rand=新的SecureRandom();
对于(int i=0;i<6;i++){
//0至9
arr[i]=兰特·耐克斯汀(10);
}

不知道是否需要另一种类型(如果需要int,请看这里:

只需使用数组即可

int[] arr = new int[6];
Random rand = new SecureRandom();
for(int i= 0; i< 6 ; i++){
    // 0 to 9
    arr[i] = rand.nextInt(10);   
}
int[]arr=newint[6];
Random rand=新的SecureRandom();
对于(int i=0;i<6;i++){
//0至9
arr[i]=兰特·耐克斯汀(10);
}

不知道是否需要另一种类型(如果需要int,请查看此处:

这将创建一个由6随机数字组成的字符串

SecureRandom test = new SecureRandom();
int result = test.nextInt(1000000);
String resultStr = result + "";
if (resultStr.length() != 6) 
    for (int x = resultStr.length(); x < 6; x++) resultStr = "0" + resultStr;
System.out.println(resultStr); // prints the 6 digit number
SecureRandom测试=新的SecureRandom();
int结果=test.nextInt(1000000);
字符串resultStr=result+“”;
如果(resultStr.length()!=6)
对于(int x=resultStr.length();x<6;x++)resultStr=“0”+resultStr;
System.out.println(resultStr);//打印6位数字

应该可以很好地工作。经过测试,它可以吐出1000个数字,并且所有数字都是6位数,包括最初选择的随机数小于100000

时的前导零。这将创建一个6随机数字字符串

SecureRandom test = new SecureRandom();
int result = test.nextInt(1000000);
String resultStr = result + "";
if (resultStr.length() != 6) 
    for (int x = resultStr.length(); x < 6; x++) resultStr = "0" + resultStr;
System.out.println(resultStr); // prints the 6 digit number
SecureRandom测试=新的SecureRandom();
int结果=test.nextInt(1000000);
字符串resultStr=result+“”;
如果(resultStr.length()!=6)
对于(int x=resultStr.length();x<6;x++)resultStr=“0”+resultStr;
System.out.println(resultStr);//打印6位数字

应该可以很好地工作。经过测试,它可以吐出1000个数字,所有数字都是6位,包括前导零,当最初选择的随机数小于100000时,您可以执行以下操作:

static String generate(int len){
   SecureRandom sr = new SecureRandom();
   String result = (sr.nextInt(9)+1) +"";
   for(int i=0; i<len-2; i++) result += sr.nextInt(10);
   result += (sr.nextInt(9)+1);
   return result;
}

您可以这样做:

static String generate(int len){
   SecureRandom sr = new SecureRandom();
   String result = (sr.nextInt(9)+1) +"";
   for(int i=0; i<len-2; i++) result += sr.nextInt(10);
   result += (sr.nextInt(9)+1);
   return result;
}
这对我有用

new SecureRandom().nextBytes(values);
int number = Math.abs(ByteBuffer.wrap(values).getInt());
while (number >= 1000000) {
     number /= 10;
}
这对我有用

new SecureRandom().nextBytes(values);
int number = Math.abs(ByteBuffer.wrap(values).getInt());
while (number >= 1000000) {
     number /= 10;
}

所以不是源代码实现资源。请带着有一些特殊问题的代码尝试来这里。因此,您需要一个用前导零填充的数字,以便其长度为6位,对吗?不。我可以是任何6位数字。
int secureNumber=secureRandom.nextInt(900000)+100000;
@shmosel分配詹姆斯·邦德代理号码的人可能不同意,因此不是源代码实现资源。带着您的代码尝试来这里,其中有一些特殊的问题,因此您需要一个填充前导零的数字,使其长度为6位,对吗?不。我可以是任何6位数字。
Int secureNumber=secureRandom.nextInt(900000)+100000;
@shmosel分配詹姆斯·邦德的代理人号码的人会不同意你知道你把它和一个问题交易联系在一起,对吗?这很相似,但不完全是在Java中,技术上是一样的,但是是的,它是另一种语言,即使语法基本上是一样的,你知道它和一个问题交易联系在一起w/C#,对吗?这很相似,但不完全是在Java中实现的技术上是一样的,但是的,这是另一种语言,尽管语法基本上与此代码片段的语法相同,这可能会提供一些直接的帮助。通过说明为什么这是一个很好的问题解决方案,正确的解释它的教育价值,并且d使其对将来有类似但不完全相同问题的读者更有用。请在您的答案中添加解释,并说明适用的限制和假设。感谢您提供此代码片段,它可能会提供一些即时帮助。通过说明这是一个很好的解决方案,正确的解释具有教育价值问题,并将使其对将来有类似但不完全相同问题的读者更有用。请您的答案添加解释,并说明适用的限制和假设。