473,326 Members | 2,126 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,326 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 18015
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.