• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creating a unique random number...

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
 
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've used the following code for this same problem in the past;

 
reply
    Bookmark Topic Watch Topic
  • New Topic