By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,733 Members | 1,137 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,733 IT Pros & Developers. It's quick & easy.

Generate a unique random string

P: n/a
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.
Jan 12 '07 #1
Share this Question
Share on Google+
9 Replies

P: n/a
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().ToByt eArray())

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
Jan 12 '07 #2

P: n/a
guarantee randomness
I of course mean uniqueness
Jan 12 '07 #3

P: n/a
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.
Marc Gravell wrote:
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().ToByt eArray())

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
Jan 12 '07 #4

P: n/a
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
Jan 12 '07 #5

P: n/a
news:u8**************@TK2MSFTNGP03.phx.gbl...
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.
Jan 12 '07 #6

P: n/a
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.

Fabio wrote:
news:u8**************@TK2MSFTNGP03.phx.gbl...
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.
Jan 12 '07 #7

P: n/a
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

"Robert Mago" <ro****@mago.comwrote in message
news:u8**************@TK2MSFTNGP03.phx.gbl...
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.

Jan 12 '07 #8

P: n/a

"DeveloperX" <nn*****@operamail.comha scritto nel messaggio
news:11*********************@a75g2000cwd.googlegro ups.com...
>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.

--
www.neodatatype.net
Jan 13 '07 #9

P: n/a
DeveloperX <nn*****@operamail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 13 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.