20

I just want to generate 6 digit random number, and the range should be start from 000000 to 999999.

new Random().nextInt(999999) is returning me number but it is not in 6 digit.

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
Kraken
  • 268
  • 1
  • 2
  • 7
  • 4
    There is no such number as `000000`. The number is `0`, which can be padded to `"000000"` in its display representation, but `0` is the same number as `000000`. If you want 6-digit numbers, you need numbers from the range of `100000` to `999999`. If you need a 6-digit *string*, generate a number almost like you did (see my comment to Karol below), then convert to string by padding to 6 digits. – Amadan Jul 13 '18 at 10:12
  • So you just want to generate number between 100000 and 999999? – GotoFinal Jul 13 '18 at 10:12
  • Is it mandatory that the number show "000000" instead of '0' when displayed? – MGT Jul 13 '18 at 10:36
  • 1
    Thanks, I got my solution. – Kraken Jul 13 '18 at 11:33
  • RandomStringUtils.randomNumeric(6); – khizerbajwa Apr 05 '21 at 18:48

5 Answers5

60

Its as simple as that, you can use your code and just do one thing extra here

String.format("%06d", number);

this will return your number in string format, so the "0" will be "000000".

Here is the code.

public static String getRandomNumberString() {
    // It will generate 6 digit random Number.
    // from 0 to 999999
    Random rnd = new Random();
    int number = rnd.nextInt(999999);

    // this will convert any number sequence into 6 character.
    return String.format("%06d", number);
}
Dev Sabby
  • 1,367
  • 1
  • 11
  • 17
  • 2
    and one more thing here, you can not get "000000" as number, in number it will be "0" only, that's why I am returning string here. – Dev Sabby Jul 13 '18 at 11:31
  • 1
    I got it, it's good. – Kraken Jul 13 '18 at 11:33
  • This code is not working. I am getting more than 6 digits some time – Suthanth DK Dec 10 '20 at 15:25
  • rnd.nextInt(999999); this will generate a random number below "999999" which will always be below 6 digit number, and then will converted by the String.format("%06d", number); into fixed 6 digit. So its not possible what you are saying. – Dev Sabby Dec 11 '20 at 10:53
16

If you need a six digit number it has to start at 100000

int i = new Random().nextInt(900000) + 100000;

Leading zeros do not have effect, 000000 is the same as 0. You can further simplify it with ThreadLocalRandom if you are on Java 7+:

int i = ThreadLocalRandom.current().nextInt(100000, 1000000)
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • 2
    899999 -> 900000, as `nextInt` is guaranteed to generate numbers less than its parameter. – Amadan Jul 13 '18 at 10:13
  • 1
    The second statement should be ThreadLocalRandom.current().nextInt(100000, 1000000) to cover maximum number of 6 digits upto 999999. – ktk Jul 12 '19 at 10:07
1

1 + nextInt(2) shall always give 1 or 2. You then multiply it by 10000 to satisfy your requirement and then add a number between [0..9999].

already solved here

public int gen() 
{ 
    Random r = new Random( System.currentTimeMillis() );
    return ((1 + r.nextInt(2)) * 10000 + r.nextInt(10000)); 
}
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
Codebender
  • 195
  • 1
  • 10
  • `public int gen() { Random r = new Random( System.currentTimeMillis() ); return ((1 + r.nextInt(2)) * 10000 + r.nextInt(10000)); }` – Codebender Jul 13 '18 at 10:19
  • You can [edit](https://stackoverflow.com/posts/51322901/edit) your answer to include additional information like your code block in the above comment. – L.Spillner Jul 13 '18 at 10:26
0

i know it’s very difficult but you can do something like this: create a class for BinaryNumber; create a constructor that generate a char[] of 6 character where every single one is generated with a randomiser from 0 to 1 override the toStrig() method so that it will return the digits char[] as a string if you want to display it. then crate a method toInt() that esaminate the string char by char with a for and turn it in a decimal base number by multiplying current digit to 10 to the pow of i:

char[] digits = {‘1’ , ‘0’ , ‘1’ , ‘1’ , ‘0’ , ‘1’};
//random 

int result = 0;
for( int i = 0; i < digits.length; i++) {
    result += Integer.parseInt(digits[i]) * Math.pow(10, i);
}

return result;
Bonfra
  • 297
  • 1
  • 11
0

This is the code in java which generate a 6 digit random code.

import java.util.*;
public class HelloWorld{

     public static void main(String []args)
     {

        Random r=new Random();
                        HashSet<Integer> set= new HashSet<Integer>();
                        while(set.size()<1)
                        {
                            int ran=r.nextInt(99)+100000;
                            set.add(ran);
                        }
                        int len = 6;
                        String random=String.valueOf(len);
                        for(int  random1:set)
                        {
                            System.out.println(random1);
                            random=Integer.toString(random1);

                        }
     }
}
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56