3

How can I generate a 6 digit integer using SecureRandom class of Java?

I am trying the following code to generate random numbers :

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

It is generating random numbers with any length including negative number. I don't find any method in SecureRandom class to provide a range of numbers. I want to generate a 6 digits positive random number

9
  • 3
    SO is not the source code implementation resource. Come here with your code attempts which have some particular problem Commented Jul 21, 2017 at 20:49
  • So you are needing a number that is padded with leading zeros so that it is 6 digits long, correct? Commented Jul 21, 2017 at 20:56
  • No. I could be any 6 digit number. Commented Jul 21, 2017 at 21:00
  • 1
    int secureNumber = secureRandom.nextInt(900000) + 100000; Commented Jul 21, 2017 at 21:16
  • 1
    @shmosel The person who assigned James Bond's agent number would disagree Commented Jul 21, 2017 at 22:10

6 Answers 6

3

Maybe using this code?

String.format("%06d", secureRandom.nextInt(999999)); 
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
2

simply done it with an array

int[] arr = new int[6]; Random rand = new SecureRandom(); for(int i= 0; i< 6 ; i++){ // 0 to 9 arr[i] = rand.nextInt(10); } 

Dont know if you need it in another type (if you want int look here: How to convert int array to int?

2 Comments

You know you linked it to a question dealing w/C#, right? It's similar, but not exact on how you'd do it in Java
Technically it works the same, but yeah its another language even tho the syntax is basically the same
2

You can do something like this:

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; } 

Test:

public static void main(String[] argv){ for(int i=3; i<25; i++) System.out.println(generate(i)); } 

Output:

639 1617 84489 440757 9982141 28220183 734679206 1501896787 29547455245 417101844095 9997440470982 24273208689568 235051176494856 9515304245005008 73519153118911442 665598930463570609 9030671114119582966 96572353350467673335 450430466373510518561 9664395407658562145827 26651025927755496179441 421157180102739403860678 

Comments

0

This will create a string of 6 random digits.

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 

Should work quite nicely. Tested so that it would spit out 1000 numbers and all of them were 6 digits long including leading zeros for when the random number initially chosen was less than 100,000.

Comments

0

This worked for me.

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

1 Comment

Thank you for this code snippet, which may provide some immediate help. A proper explanation would greatly improve its educational value by showing why this is a good solution to the problem, and would make it more useful to future readers with similar, but not identical, questions. Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply.
0

This is how you would do it using Java IntStream

private final SecureRandom secureRandom = new SecureRandom(); public String generateCode() { return IntStream.iterate(0, i -> secureRandom.nextInt(10)) .limit(6) .collect(StringBuilder::new, StringBuilder::append, StringBuilder::append).toString(); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.