6

In my application, i want to generate 9 digit random numbers such that they r unique. Only one 9 digit random number should be generated each time i run the application and it should be unique. Later i want to save this number along with its associated data in a .txt file so that i can retrive the data associated with this unique number when required. How should i achieve this?

Java D
  • 436
  • 1
  • 8
  • 18
bhabs
  • 133
  • 1
  • 2
  • 9
  • Yu could use the file size of your file _before_ you write the new information, and pad it with 0s on the left. In this way, all numbers in that file will be unique. – Ingo Apr 06 '11 at 10:31
  • check out this post: http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript – jwerre Jun 15 '12 at 13:45

5 Answers5

18

Do you want them to be truly random or truly unique? You can only have one of these.

If you want them to be truly random, then just randomly pick 9 digits from 0-9 and construct them into your number. There will be a small chance of a duplication, especially over larger numbers of iterations. It will be truly random, though.

If you want them to be truly unique, then you have to store every one in a database to ensure there are no duplicates. If you generate a duplicate, you'll need to regenerate or just increment the number and try again. If this is what you're looking for, it might be good to try just incrementing the value starting at one.

Erick Robertson
  • 32,125
  • 13
  • 69
  • 98
13

For unique number try: (new Date()).getTime() it will never be the same unless you are generating multiple number in a sec.

Harry Joy
  • 58,650
  • 30
  • 162
  • 207
  • I don't know who -1'd this, but this is actually a valid point (as long as you don't generate a lot of numbers per second). – Roy T. Apr 06 '11 at 10:25
  • 1
    @Roy: Even I'm wondering the same. Why someone has downvoted this. – Harry Joy Apr 06 '11 at 10:26
  • 2
    -1 This will not generate nine digit random numbers. It won't even generate random numbers. It just gets the current time in milliseconds. How is this anything like what the OP is asking for? I'm wondering why anyone has upvoted this. – Erick Robertson Apr 06 '11 at 10:27
  • 3
    I did not downvote it, but note that it's just not true that getTime() is guaranteed to return a unique number. This is true only under the assumption that nobody manipulates the clock. – Ingo Apr 06 '11 at 10:28
  • @Erick: You can easily crop 9 digits from it. its not a big deal. – Harry Joy Apr 06 '11 at 10:30
  • @Harry: Those 9 digits aren't guaranteed never to be the same, and they certainly aren't random. – Erick Robertson Apr 06 '11 at 10:31
  • @Erick: yes they are not random. But in most cases they are unique and thats why stated in answer "for unique number try....". – Harry Joy Apr 06 '11 at 10:37
  • Unique and random can't be guaranteed. It is possible to use randomness to get almost uniqueness. But that is no guarantee. Therefore, I'd say it is no problem that it is not random. But, the time is of course easy to guess. That might be a problem in some applications. – jmg Apr 06 '11 at 10:46
  • 1
    With all due respect, this method really shouldn't be offered as a way to generate anything that may be called "unique" or "random." It doesn't come remotely close to achieving either of these qualities. This really shouldn't be cross-linked into new questions on the same topic, because it truly could send a new programmer in the completely wrong direction. I offer my opinion simply as a concerned citizen and hope it is clear I'm trying to give constructive feedback. – Sean Mickey Apr 27 '12 at 21:26
8

You could combine the Random Class & system time (or a function using system time) e.g.

  Random random = new Random(System.nanoTime());

  int randomInt = random.nextInt(1000000000);

You could also use some function on the system time e.g.

  Random random = new Random(System.nanoTime() % 100000);

  int randomInt = random.nextInt(1000000000);
Bruce Martin
  • 10,358
  • 1
  • 27
  • 38
2

If you just want a random 9-digit number, try:

long number = (long) Math.floor(Math.random() * 900000000L) + 10000000L;

However, if you just want an unique number I'd go for using a database instead.

Andreas Johansson
  • 1,135
  • 6
  • 23
1

What you're asking for is a unique identifier, that can never be repeated, this is by definition not a random number.

What you're probably looking for are GUIDs, but these are a lot longer than 9 digits (because the chance on generating the same number twice with only 9 digits is quite large if you look at it from a global scale).

Anyway check this wikipedia article: http://en.wikipedia.org/wiki/Globally_unique_identifier

Edit: to clarify, GUIDs have somewhate of a random nature.

Roy T.
  • 9,429
  • 2
  • 48
  • 70