Win a copy of The Career Toolkit this week in the Jobs Discussion forum!
  • 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 ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
  • salvin francis
  • Frits Walraven
  • Piet Souris

Unique number generation

Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to generate a unique random number.
But the constraint is this number must not be repeated in 10 months span.
it should be unique and itz length must be 9 digits long.
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, kasi!
In 10 months time, about how many ids are to be generated?
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI kasi
As Dirk pointed out, it would more helpful to know "how many IDs you need to generate" BUT here is some rough logic I can think of right now,
If we have 9 digit number to be generated that should not be repeated in 10 months span then I would suggest something like following,
- Consider positions of digits from 0 to 8, left-to-right (total 9 digits).
- First digit (0th) you can use to indicate the month number,
0 for 1st month
1 for 2nd month
2 for 3rd month etc...
- Second two digits (1st and 2nd) to indicate the day in the month (00 to 31)
- For remaining 6 digits (3rd to 8th position) you can generate random number.
This way atleast we would ensure that in a single day in any month we can generate 10^6 random numbers and we reduce our scope of "having uniqueness" to 10^6 then 10^9.
Now, this would not be really fully random as you would know the first 3 numbers always but I am just throwing ideas from top of my head if they are useful in anyway...
I wish I was a mathemetician
His brain is the size of a cherry pit! About the size of this ad:
the value of filler advertising in 2020
    Bookmark Topic Watch Topic
  • New Topic