Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java random number with given length [duplicate]

Tags:

java

random

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 ?

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?

EDIT 2:

This is my final solution. Thanks for all the help guys!

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; } 
like image 530
Marthin Avatar asked Mar 22 '11 14:03

Marthin


People also ask

How do you generate a random number of a specific length in Java?

Method 1: Using Math. random() Here the function getAlphaNumericString(n) generates a random number of length a string.

How do you generate a 15 digit unique random number in Java?

Random random = new Random(); int rand15Digt = random. nextInt(15);

How do you generate a 6 digit unique random number in Java?

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.


2 Answers

To generate a 6-digit number:

Use Random and nextInt as follows:

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.

So how do I randomize the last 5 chars that can be either A-Z or 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(); 
like image 160
aioobe Avatar answered Oct 07 '22 06:10

aioobe


Generate a number in the range from 100000 to 999999.

// pseudo code int n = 100000 + random_float() * 900000; 

For more details see the documentation for Random

like image 44
Bombe Avatar answered Oct 07 '22 08:10

Bombe