aspose file tools*
The moose likes Beginning Java and the fly likes Creating a unique random number... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating a unique random number..." Watch "Creating a unique random number..." New topic
Author

Creating a unique random number...

Ben Tigger
Greenhorn

Joined: May 26, 2004
Posts: 7
Basically I have an array full of objects customer, one attribute a customer must have is a unique account number.

//make me a random number generator called 'generator'
Random generator = new Random();

//Set account number to random number
AccountNumber = generator.nextInt(89999);

//Make sure number is 5 digits so if randome number is, 0 + 10000 = 10000
accountNumber = accountNumber + 10000;

Now this creates the random number for the account but I have no way of making it unique.

I thought about listing through all the previous account numbers and then if it was the same then picking a new random number.. something like..

for (int x = 0, customerArray.getlength-1, x++)

{

if accountNumber == customerArray[x].getAccountNumber();

AccountNumber = generator.nextInt(89999);
accountNumber = accountNumber + 10000;

}


I only just roughtly wrote that code down so dont point out any small mistakes i might of made, anyway doing the above method would check through all previous account numbers and if its the same the do a new random number, however the new random number could be the same as an account already passed so this doesnt really help...

anyone here got any ideas

Thanks alot.. oh in case you can't tell i'm writing this at 12 at night half asleep so im not sure about grammatical errors or the like...
Ben Tigger
Greenhorn

Joined: May 26, 2004
Posts: 7
Gr8....

//make me a random number generator called 'generator'
Random generator = new Random();

//Set account number to random number
accountNumber = generator.nextInt(89999);
//Make sure number is 5 digits so if randome number is, 0 + 10000 = 10000
accountNumber = accountNumber + 10000;

do
{
for (int x = 0; x < customerArray.length; x++)

{
if (accountNumber == customerArray[x].getAccountNumber())

{
accountNumber = generator.nextInt(89999);
accountNumber = accountNumber + 10000;

newRandom = true;
}
}

}
while (newRandom = true);

I thought that would work but now it just freeze at the point it enters the do, while, loop... im tired.. wont somone please help me....
Tim West
Ranch Hand

Joined: Mar 15, 2004
Posts: 539
Does it have to be random?

One easy way to create unique account numbers is to have a class with a static method that generates them. I haven't tested the stuff below, but off the cuff:



This is obviously a very simple implementation - if the numbers have to be unique across subsequent runnings of your program, you'll have to store the value somewhere and initialise the generator appropriately.

Hope that helps...though if you really wanted random, I guess it won't


--Tim
Ben Tigger
Greenhorn

Joined: May 26, 2004
Posts: 7
Well, this is just some coursework and the program simulates one day of a car hire place.. adding new customers, cars and the like so I don't need to store the details anywhere, although there is another programme i will have to do that does need to have this feature... anyway with this one I really do need to just create a unique random number..

meh.. i'm just not sure why my do, while loop seems to freeze.. it's probably something stupid like a missing ; or too many } but I can't spot it....
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

a) split it into functions with clear tasks.
b) implement them
c) use [ code]-Blocks (Buttons) to make code readable.



using a customerArray of ints isn't very useful - you have to know the maximum size in advance, and it's quiet big, iterating takes much time...

If you use a ArrayList, it will grow by every new customer, and is small in the beginning, and fast. And you learn about ArrayList which is of much benefit.

Second solution with array would be better:
boolean usedNumber = new boolean [10000];
Here you could use the number as index, which is damned fast.

[ May 26, 2004: Message edited by: Stefan Wagner ]

http://home.arcor.de/hirnstrom/bewerbung
Tim West
Ranch Hand

Joined: Mar 15, 2004
Posts: 539
Your code never sets newRandom to false, when it decides a number is OK. Also, you have a logic error - if you have a "clash", you need to start the checking again at the start of the array. Something like this:



Just a point, in most situations randomness isn't too important (except maybe for credit card numbers). You just generate a sequence of some sort, that's guaranteed to be unique. Databases do this to get primary key columns if you use auto sequence generating. The biggest problem with randomness (aside from it not being necessary) is that it's very computationally expensive to create a unique number that's truly random. If you had a million records, your code there would be very slow, since each time it has to iterate through all the records at least once.

Seeing it's an assignment, I wouldn't make life any harder for yourself than you have to. Unless it says "random" anywhere, just go with a sequence...

Anyway, feel free to ignore all that....hope you work it out though.



--Tim
Ben Tigger
Greenhorn

Joined: May 26, 2004
Posts: 7
This is the way I did it...



The main problem was the while at the bottom.. I has while newRandom = true where is should be while newRandom == true..

[ edited to remove the evil tab character, and to remove a lot of extra white space -ds ]
[ May 27, 2004: Message edited by: Dirk Schreckmann ]
Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
I've used the following code for this same problem in the past;



Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating a unique random number...