0

How to generate random 4-character key from these characters: [a-zA-Z0-9]
That is less likely to collide, like a unique ID?

Also having those 62 characters by calculation would result for about 14.7 million keys.

Is there a way to stretch that limit?

quarks
  • 33,478
  • 73
  • 290
  • 513
  • 2
    Without changing the allowable characters or total number of characters, no. You can't magically "stretch" a 1 foot chain into a 2 foot chain without changing something. – Marc B Nov 15 '11 at 14:24
  • 2
    possible duplicate of [How to generate a random alpha-numeric string in Java](http://stackoverflow.com/questions/41107/how-to-generate-a-random-alpha-numeric-string-in-java) – Oliver Charlesworth Nov 15 '11 at 14:26
  • The only way I can think of to "stretch" the limit is to "recycle" no longer used ids – Liviu T. Nov 15 '11 at 14:46

1 Answers1

1

The maximum possible keys is 62^4. To make this larger you need more possible characters or a longer length.

The simplest way to generate unique ids is to use a counter starting with 0..9, A-Z, a-z etc.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130