Generate a unique random string

  • Thread starter Thread starter Robert Mago
  • Start date Start date
R

Robert Mago

Is there a way to create a 10 characthers or less, alph-numeric string which
is unique. I can't use the guid since its longer then 10 characthers. Also i
cannot use a random number, since being random does not mean that its
unique.
 
Well, yes and no... guid also doesn't /technically/ guarantee
randomness... however, as a quick option you could perhaps consider
taking a substring of something like:

System.Convert.ToBase64String(Guid.NewGuid().ToByteArray())

This is always alpha-numeric and fairly unique / random-esque...

But if you want uniquess, then use something like an identity column /
counter, or use a database / dictionary / whaever to store the used
values.

Marc
 
Yep I'm with Marc, ideally use a database which can issue the next
unique ID (make sure your locking is sorted, you don't want two
concurrent requests returning the same ID), or a web service or
similar. If you're not going to be generating new ID's that frequently
you can encode the time it's generated to the nearest millisecond into
a string. A literal string representation would be too long, so you
would need to encode it.
YYMMDDHHMMSSmmm is 15 chars long, but if you think about it, YY is
going to be 07-99 assuming your software runs for 92 years but lets say
07-30, MM is 1-12 for month, 0-59 for minutes. DD 1-31 SS = 0-59 mmm =
0 to 999.
So you can represent YY with one char by using A for 07 and Z for 33
for example
0-59 is easy if you just add 32 you'll get a nice printable character,
so now we're down to
YMDHMSmmm with one left over.

I'd still recommend the server route though.
 
Another correction; *almost* alphanumeric ;-p I forgot about "+", "/",
"="

You get the idea... but again stress that a database is the way to go
here...

Marc
 
Is there a way to create a 10 characthers or less, alph-numeric string
which is unique.

No, because mathematically with a finite set of items you can have only a
finite set of combinations.

You can get uniquety (?) if you have some external limits, i.e. if you know
that is just one PC that produces the string the string can be a number
betwen 0000000000 and 9999999999.
 
A finite set can consist of unique values. a byte can hold 256 unique
values, a bit exactly 2.
A 10 character string restricted purely to numbers can hold
10,000,000,000 unique values.
So the correct answer is yes.
T'internet can teach people a lot here when you consider how IP
addresses work.
If you have one provider of unique ID's you could start at 1 and just
increment. If you want each of a hundred workstations to be able to
generate unique ID's you simply subnet your address space.
For one provider : NNNNNNNNNN
For 10 providers : SNNNNNNNNN
For 100 providers : SSNNNNNNNN
Where N is the ID and S is the provider's ID.
 
I'm repeatedly surprised by how people go off answering without any idea
about why someone want something. The "identity column" replies assume (I
don't see you mentioning it) that you intend to use this as an ID of some
sort. And furthermore that "unique to the table" is good enough.

What do you want these things for? Clearly you know there is "a way" to
create 10-character alphanumeric strings and you must realize that
uniqueness can only be insured by testing whether the string you generate is
a member of a set of strings you generated in the past.

A GUID while not "guaranteed" to be unique is considered unique (across the
universe) due to it's huge range of values. If you can generate several
billion and not get a duplicate then we're in the odds of being hit by
lightning "in the next minute" sort of area.

The way you can guarantee a unique 10-character sequence is to generate all
10-character sequences and then allot them as needed. Regardless of the
mechanism you use to do it simply mark each one as "used" as you use each of
them. Now you have a unique value, a total number of unique values, the
total number of unique values and (if you care to enhance the system) the
rate at which you consume values.

Tom
 
A finite set can consist of unique values. a byte can hold 256 unique
values, a bit exactly 2.
A 10 character string restricted purely to numbers can hold
10,000,000,000 unique values.
So the correct answer is yes.

The correct answer depends on the meaning of "unique" for Robert, so
MATHEMATICALLY when an option set the sentence false the answare is "no".

As I sayd, you can get an unique ID only if you have the control on the
generator algorithm.
The identity field on a table can be valid only for clients connected to
that DB, you cannot guarantee an unique ID from other clients.

If "unique" is between a range or not is not really significative.
 
DeveloperX said:
A finite set can consist of unique values. a byte can hold 256 unique
values, a bit exactly 2.

Yes - but the fact that it's only got 256 unique values means that
after you've generated 255 unique values "randomly", the last unique
value *cannot* be random (i.e. it's completely predictable) and after
that you can't generate any more.
A 10 character string restricted purely to numbers can hold
10,000,000,000 unique values.
So the correct answer is yes.

Well, it's "yes" unless you want to be able to guarantee uniqueness
over more than 10,000,000,000 values... (using digits). That's the
problem with specifying something generating "unique" values: you need
to know the context.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top