• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

java uniqueId creation  RSS feed

 
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 ..
 
Bartender
Posts: 11445
18
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.
 
Marshal
Posts: 64494
225
  • 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.
 
Rancher
Posts: 4686
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.
 
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.
 
Author
Posts: 3443
  • 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
 
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.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just use a timestamp. Always unique.
 
Campbell Ritchie
Marshal
Posts: 64494
225
  • 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?
 
Sheriff
Posts: 21744
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor 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.
 
Your buns are mine! But you can have this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!