473,396 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Generate a unique random string

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
9 18044
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
guarantee randomness
I of course mean uniqueness
Jan 12 '07 #3
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
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
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
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Andreas Lobinger | last post by:
Aloha, i wanted to ask another problem, but as i started to build an example... How to generate (memory and time)-efficient a string containing random characters? I have never worked with...
2
by: Phipps Xue | last post by:
Hello All, I wanna write an small game which need to generate a random string each time. It should go like this. TCHAR tmpStr; unsigned int nLen=50; TCHAR seedStr=_T("dummy seed"); int...
4
by: Jonathan Burd | last post by:
Greetings everyone, Here is a random string generator I wrote for an application and I'm wondering about the thread-safety of this function. I was told using static and global variables cause...
2
by: Juan | last post by:
Hi How to generate an unique random string of determinate characters length? Thanks
12
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
1
by: Jen2 | last post by:
hello, help. i'm trying to generate a random string of characters of user defined length, with or without repetitions. i've just started using C and have absolutely no idea what i'm doing.
3
by: miladhatam | last post by:
hi , how can i generate a random string like this "a8t32zx1bg3" i know that about Number random.next(x) does it work without Number ? thanks
38
by: Andrea | last post by:
Hi, Anyone could me suggest how to create a function that generates a random string? The function should be: int _RandomString(char** str,int len); so, it takes an empty string str and it puts...
5
by: ashurack | last post by:
I found a stored procedure online a while back and want to inplement it. The only problem is that it doesn't check to see if the number generated is currently in use in the DB. I know it's really...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.