my dog learned polymorphism*
The moose likes Beginning Java and the fly likes java uniqueId creation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java uniqueId creation" Watch "java uniqueId creation" New topic
Author

java uniqueId creation

vinitha simon
Ranch Hand

Joined: Dec 13, 2007
Posts: 64
My requirement is

1) A file will be created and will be feeded as input to another system which will read the data's and will insert into DB . My requirement is when
ever i create the file i should have three fields(lets say field1,field2,field3) with unique number .

a) field1 can have 5 digits

b) field2 can have 13 digits .

c) feild3 can have 10 digits .


All 3 fields should be numeric . There will be 10 users . These fields should be unique when ever they generate the file. Any solution is
higly appreciated ..
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10175
    
    8

Check out java.util.Random
It has methods like nextInt and nextLong which will be useful to you.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38108
    
  22
You can't be certain that random numbers will be unique. It might be possible to use primary keys from the database.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Smells like homework to me, so here is a hint.

How serious are your "unique" requirements? As posted up thread, Random will give you numbers that may be unique.

Do you mean unique within a run? or across runs?

If it has to be across runs, you need some permanent store, and a database is really nice since all good ones will also assign a unique value for you.

If its just within a run, learn about HashSet.
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
As I understand the requirement. there are 10 users, and all of them can generate a file for themselves containing 3 numbers. And these numbers should be unique to a file i.e. they should not be in a file generated by some other user. And I am assuming that the numbers in the file must be unique w.r.t. database too in which they will finally be inserted.

Since this is a multi-user system, I guess you need to maintain a unique number generator which is thread safe(since multiple users can invoke it at the same time). This unique number generator could either use a permanent storage(like database table) to fetch a unique number or could fetch it 'in-memory' by maintainig counters or something similar. The key thing would be to maintain the thread safety of the unique number generator.


Thanks and Regards
arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3219
Have a look at MersenneTwister.







Java Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
Hi Arulk
I agree Mersenne twister would generate very high-quality pseudorandom numbers. My only doubt is, how are you ensuring that the same number would never be generated again

TIA
Graeme Byers
Ranch Hand

Joined: Apr 16, 2004
Posts: 127
Use a sequence number :
For Oracle Oracle :www.cs.rpi.edu/~sibel/dbs/FALL2003/system_info/oracle/unique.htm
DB2 :www.ibm.com/developerworks/db2/library/techarticle/0205pilaka/0205pilaka2.html
MySql ://forums.mysql.com/read.php?61,143867,143867.

NEVER EVER attempt to implement your own sequence numbers in any data base - performance will be degraded by a task locking the sequence number while other tasks wait for the lock to be released.
Don Solomon
Ranch Hand

Joined: Jul 20, 2008
Posts: 48
Just use a timestamp. Always unique.


Software development is an exercise in thinking not coding.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38108
    
  22
Originally posted by Don Solomon:
Just use a timestamp. Always unique.
Even if two instances are executed simultaneously on different computers?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Although the chances are very very slim if you use System.nanoTime()*, it is indeed possible. You would need to include an ID for the computer itself to somehow.


* System.currentTimeMillis() is not accurate enough by a longshot.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java uniqueId creation
 
Similar Threads
valid when
overiding equals() method
Is it possible in Java IO
how can use conditional validations on predefined validations in struts1.2
Displaying Table in Expanded and Collapsed manner