# Generate users id

• October 5th, 2011, 02:30 PM
sante
Generate users id
hello guys,

I am doing my own project and i have reach a point that i need to generate users id with format 000000 up to 999999 without allowing any number to repeat its self. How can i do that with java?
• October 5th, 2011, 03:10 PM
kenster421
Re: Generate users id
You should choose the location of where you post more carefully. This should be in the general questions area.

Create a recursive function that generates a random number between 0 and 9 and then feeds that number to the next recursion, but specify that the particular number (x) that your received in the previous step cannot be the next number generated. If so, then generate the number again.

Read up on recursion here: Recursion - Wikipedia, the free encyclopedia

Code Java:

```public String generateNumberRecusively(int numDigits) { return getNextNumberHelper("x", 6, 0); }   public String getNextNumberHelper(String previousNumber, int numDigits, int digitCount) { if(previousNumber.equals("x")||) {} //first iteration, TO-DO... generate a number 0-9, then call self and increment counter else if(digitCount == numDigits) // generate a number 0-9 but not previousNumber then return previosNumber + generatedDigit {} else // generate a number 0-9 but not previousNumber then call self and increment counter {} }```
• October 5th, 2011, 03:21 PM
copeg
Re: Generate users id
I have moved your post to a more appropriate forum.

I'm not sure if kenster421's advice actually solves your problem - it seems it solves something else entirely.

You can do this 2 (and probably many more) ways. First, just loop up from 0000000 onwards. Second, Create a Set and generate a random number between those specified - checking the Set to see if that number has already been generated - if not continue, if so generate another number. Try writing some code, and post back if you have more problems.
• October 6th, 2011, 09:30 AM
kenster421
Re: Generate users id
Quote:

Originally Posted by copeg
I have moved your post to a more appropriate forum.

I'm not sure if kenster421's advice actually solves your problem - it seems it solves something else entirely.

You can do this 2 (and probably many more) ways. First, just loop up from 0000000 onwards. Second, Create a Set and generate a random number between those specified - checking the Set to see if that number has already been generated - if not continue, if so generate another number. Try writing some code, and post back if you have more problems.

If you read his post, you would see that he needed to generate a six digit number with no repeats. The solution I tried to steer him to was to generate the number, digit by digit, making sure that the previous digit used will not be the next digit, i.e., no repeats. Not only does it do that, but you can specify what length you want the number to be. I am unsure as to what "other" problem my code solves.
• October 6th, 2011, 09:44 AM
copeg
Re: Generate users id
Quote:

Originally Posted by kenster421
If you read his post, you would see that he needed to generate a six digit number with no repeats. The solution I tried to steer him to was to generate the number, digit by digit, making sure that the previous digit used will not be the next digit, i.e., no repeats. Not only does it do that, but you can specify what length you want the number to be. I am unsure as to what "other" problem my code solves.

Then it is up to the original poster to clarity...because I read the initial problem as to generate user id's which are represented by 6 digits - and no user id can be a duplicate of another (as opposed to no digit within the user id can be duplicate)
• October 6th, 2011, 12:29 PM
kenster421
Re: Generate users id
Quote:

Originally Posted by copeg
Then it is up to the original poster to clarity...because I read the initial problem as to generate user id's which are represented by 6 digits - and no user id can be a duplicate of another (as opposed to no digit within the user id can be duplicate)

Too true. If it is the case that you interpreted it as, then a simple incremental counting variable would have sufficed.