Q:

java- how to generate a 6 digit random hexadecimal value

I have a scenario in a Android app, where a random hexadecimal value has to be generated with 6 digits.  (The range of values can be hexadecimal or integer values).
What is the most efficient way to do this?
Do I have to generate a random decimal number, and then convert it to hexadecimal? Or
 can a value be directly generated?

A:

    String zeros = "000000";
    Random rnd = new Random();
    String s = Integer.toString(rnd.nextInt(0X1000000), 16);
    s = zeros.substring(s.length()) + s;
    System.out.println("s = " + s);

A:

You can use hex literals in your program the same way as decimal literals. A hex literal is prefixed with 0x. Your max value is FFFFFF, so in your program you can write
int maxValue = 0xFFFFFF;

Then you need to generate random numbers in that range. Use the Random class as you normally would.
Random r = new Random();
int myValue = r.nextInt(maxValue + 1);

Note the use of maxValue + 1, because the upper bound for nextInt() is exclusive.
The final step is to print out your hex value.
System.out.printf("%06X", myValue);

