• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java uniqueId creation

 
vinitha simon
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11064
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out java.util.Random
It has methods like nextInt and nextLong which will be useful to you.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
arulk pillai
Author
Ranch Hand
Posts: 3387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at MersenneTwister.





 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just use a timestamp. Always unique.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Don Solomon:
Just use a timestamp. Always unique.
Even if two instances are executed simultaneously on different computers?
 
Rob Spoor
Sheriff
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic