469,280 Members | 2,045 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,280 developers. It's quick & easy.

Generate random string

Hi all,

I need to be able to create some license keys for an application. Ideally
these wouldn't be too long in length (ie, easier to remember/type in), but I
would like them to be auto generated and a check made to see whether or not
that key has already been used, something which could generate up to about
10000 keys (or more if the string doesnt have to be too long) would be very
useful.

A combination of letters, numbers, upper and lower case is needed.

I could sit here and start plugging away at this, an array for numbers, an
array for letters, a random test for uppercase/lowercase etc, but it occured
to me that I'm probably just reinventing the wheel a bit here, someone must
have done this before!

Has anyone produced anything similar to this in the past?

Any help would be appreciated.

Regards

Rob
Jul 19 '05 #1
18 9553
Why not just use a GUID?

--
http://www.aspfaq.com/
(Reverse address to reply.)


"Rob Meade" <ro********@NO-SPAM.kingswoodweb.net> wrote in message
news:9n********************@news-text.cableinet.net...
Hi all,

I need to be able to create some license keys for an application. Ideally
these wouldn't be too long in length (ie, easier to remember/type in), but I would like them to be auto generated and a check made to see whether or not that key has already been used, something which could generate up to about
10000 keys (or more if the string doesnt have to be too long) would be very useful.

A combination of letters, numbers, upper and lower case is needed.

I could sit here and start plugging away at this, an array for numbers, an
array for letters, a random test for uppercase/lowercase etc, but it occured to me that I'm probably just reinventing the wheel a bit here, someone must have done this before!

Has anyone produced anything similar to this in the past?

Any help would be appreciated.

Regards

Rob

Jul 19 '05 #2
Rob Meade wrote:

I need to be able to create some license keys for an application.
Ideally these wouldn't be too long in length (ie, easier to
remember/type in), but I would like them to be auto generated and a
check made to see whether or not that key has already been used,
something which could generate up to about 10000 keys (or more if the
string doesnt have to be too long) would be very useful.

A combination of letters, numbers, upper and lower case is needed.

I could sit here and start plugging away at this, an array for
numbers, an array for letters, a random test for uppercase/lowercase
etc, but it occured to me that I'm probably just reinventing the
wheel a bit here, someone must have done this before!

Has anyone produced anything similar to this in the past?


This will do far more than 10,000, and will give you uniqueness:

GUID = Server.CreateObject("Scriptlet.Typelib").GUID


--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
Jul 19 '05 #3
Thanks chaps, but how will it know its 'unique'? ie, how will it remember
that it hasnt already used that one? I'll try the code in a minute or two -
but if you happen to reply prior to that - how many characters does this
generate by the way?

Thanks again,

Rob
Jul 19 '05 #4
Ok - so I got this:
Jul 19 '05 #5
(I hate ctrl+enter!!)...

I got this...

FFDA9960-8C53-497F-9D8F-A736352F21C0

That and a password is what a user will need to enter when logging into a
members area to download any updates to an application - bit long - hard to
remember etc...(great for uniqueness mind!)...

Thoughts/suggestions...

Rob
Jul 19 '05 #6
Well, GUID stands for Globally Unique IDentifier, so you can do the math
from there...

Keep this in mind, of course, if you are going to generate or response.write
from ASP:
http://www.aspfaq.com/2358

--
http://www.aspfaq.com/
(Reverse address to reply.)


"Rob Meade" <ro********@NO-SPAM.kingswoodweb.net> wrote in message
news:u7****************@news-text.cableinet.net...
Thanks chaps, but how will it know its 'unique'? ie, how will it remember
that it hasnt already used that one? I'll try the code in a minute or two - but if you happen to reply prior to that - how many characters does this
generate by the way?

Thanks again,

Rob

Jul 19 '05 #7
Why does a serial # have to be easy to remember? Do you remember your
Windows product key, your Photoshop serial number, hell your credit card
number?

If you want something easy to remember, then toss this random string
generation out the window, and make users choose their own password!

--
http://www.aspfaq.com/
(Reverse address to reply.)


"Rob Meade" <ro********@NO-SPAM.kingswoodweb.net> wrote in message
news:S9****************@news-text.cableinet.net...
(I hate ctrl+enter!!)...

I got this...

FFDA9960-8C53-497F-9D8F-A736352F21C0

That and a password is what a user will need to enter when logging into a
members area to download any updates to an application - bit long - hard to remember etc...(great for uniqueness mind!)...

Thoughts/suggestions...

Rob

Jul 19 '05 #8
"Aaron [SQL Server MVP]" wrote ...
Why does a serial # have to be easy to remember?
Because they'll be using it often for logging in purposes, hence in my first
post suggesting that it didnt have too many characters.
Do you remember your Windows product key, your Photoshop serial number,
Absolutely not - agree fully with you there, but I dont need to enter those
every time I want to download a patch/fix etc..
hell your credit card number?
That one I do know - but my card kinda glows red so its easy to see :o)
If you want something easy to remember, then toss this random string
generation out the window, and make users choose their own password!


They'll have a password as well as the key Aaron, the licence number I guess
is affectively their username.

Regards

Rob
Jul 19 '05 #9
"Aaron [SQL Server MVP]" wrote ...
Well, GUID stands for Globally Unique IDentifier, so you can do the math
from there...
Whilst not really wanting to go down this road, but feeling propelled to say
it....there must be an upper limit to the number of combinations though
surely - if I do the response.write GUID for example and then held F5 down
on my PC lets say for the rest of my life time I'm surely going to get a
duplicate eventually???

I'm unaware (and you might have to excuse my ignorance a little as Maths was
never a strong subject for me) - of anything thats simply endless - apart
from space - but even that must have boundaries somewhere...(wooow - brain
is starting to melt a bit now)..
Keep this in mind, of course, if you are going to generate or response.write from ASP:
http://www.aspfaq.com/2358


Thanks for that, I didnt even know about the GUID ability in ASP so I've
learnt something new tonight :o)

Rob
Jul 19 '05 #10
> They'll have a password as well as the key Aaron, the licence number I
guess
is affectively their username.


Then I suggest using their e-mail address as the username (which is actually
quite common). *Anything* you generate for them is going to be another
hard-to-remember string that they're going to write on a sticky, forget,
etc.

A
Jul 19 '05 #11
The number of combinations is 36 ^ 32. Think about this for a second... 36
^ 6 exceeds the upper boundary for an INT datatype (~2 billion). 36 ^ 13
exceeds the upper boundary for a BIGINT datatype
(9,223,372,036,854,775,807). So take that latter number, square it, and
then multiply that by 2 billion.

This is no RAND(), trust me.

--
http://www.aspfaq.com/
(Reverse address to reply.)


"Rob Meade" <ro********@NO-SPAM.kingswoodweb.net> wrote in message
news:jN*****************@news-text.cableinet.net...
"Aaron [SQL Server MVP]" wrote ...
Well, GUID stands for Globally Unique IDentifier, so you can do the math
from there...
Whilst not really wanting to go down this road, but feeling propelled to

say it....there must be an upper limit to the number of combinations though
surely - if I do the response.write GUID for example and then held F5 down
on my PC lets say for the rest of my life time I'm surely going to get a
duplicate eventually???

I'm unaware (and you might have to excuse my ignorance a little as Maths was never a strong subject for me) - of anything thats simply endless - apart
from space - but even that must have boundaries somewhere...(wooow - brain
is starting to melt a bit now)..
Keep this in mind, of course, if you are going to generate or

response.write
from ASP:
http://www.aspfaq.com/2358


Thanks for that, I didnt even know about the GUID ability in ASP so I've
learnt something new tonight :o)

Rob

Jul 19 '05 #12
Aaron [SQL Server MVP] wrote on 09 jun 2004 in
microsoft.public.inetserver.asp.general:
The number of combinations is 36 ^ 32. Think about this for a
second... 36 ^ 6 exceeds the upper boundary for an INT datatype (~2
billion). 36 ^ 13 exceeds the upper boundary for a BIGINT datatype
(9,223,372,036,854,775,807). So take that latter number, square it,
and then multiply that by 2 billion.

This is no RAND(), trust me.


rant() perhaps?

Main Entry: rant
Pronunciation: 'rant
Function: verb
Etymology: obsolete Dutch ranten, randen
intransitive senses
1 : to talk in a noisy, excited, or declamatory manner
2 : to scold vehemently
transitive senses : to utter in a bombastic declamatory fashion

================

But then, Aaron, we trust you.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 19 '05 #13
> 1 : to talk in a noisy, excited, or declamatory manner
2 : to scold vehemently
transitive senses : to utter in a bombastic declamatory fashion

================

But then, Aaron, we trust you.


I don't think I have submitted examples of either 1. or 2. in this thread.
It's pretty simple math, really.

A
Jul 19 '05 #14
Aaron [SQL Server MVP] wrote:
...surely - if I do the response.write GUID for example and then
held F5 down on my PC lets say for the rest of my life time I'm
surely going to get a duplicate eventually???

The number of combinations is 36 ^ 32. Think about this for a
second... 36 ^ 6 exceeds the upper boundary for an INT datatype (~2
billion). 36 ^ 13 exceeds the upper boundary for a BIGINT datatype
(9,223,372,036,854,775,807). So take that latter number, square it,
and then multiply that by 2 billion.


To be fair, his computer cannot generate all 36^32 values, as he has a very
small subset of the MAC address space. Holding down F5 would run him through
his remaining number of timestamps, but certainly not the entire GUID space.

--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
Jul 19 '05 #15
"Aaron [SQL Server MVP]" wrote ...
Then I suggest using their e-mail address as the username (which is actually quite common). *Anything* you generate for them is going to be another
hard-to-remember string that they're going to write on a sticky, forget,
etc.


Hi again Aaron,

I'd not actually thought of using their email address, figured it would look
more 'professional' to fire out a licence key with the app (not that it
means much I guess!)...anyway, I'll do that - cheers for the suggestion and
GUID help :)

Rob
Jul 19 '05 #16
"Aaron [SQL Server MVP]" wrote ...
The number of combinations is 36 ^ 32. Think about this for a second... 36 ^ 6 exceeds the upper boundary for an INT datatype (~2 billion). 36 ^ 13
exceeds the upper boundary for a BIGINT datatype
(9,223,372,036,854,775,807). So take that latter number, square it, and
then multiply that by 2 billion.
Again, being that my maths is pants, I'm assuming the ^ is "to the power
of" - not really sure what that means either, but I just love the fact that
the number is over 9 million, million, million * 2 billion! ROTFL! Wonder
why they stopped there???? You think someone thought - "nah, they'll never
need anything bigger than that, I'm sure...." - hehe....one day though...one
day :oD
This is no RAND(), trust me.


hehe

Cheers - I've had quite a chuckle here :o)

Rob
Jul 19 '05 #17
"Dave Anderson" wrote ...
To be fair, his computer cannot generate all 36^32 values, as he has a very small subset of the MAC address space. Holding down F5 would run him through his remaining number of timestamps, but certainly not the entire GUID

space.

*WHOOOOOOOOOOOOOSH*

LOL...not sure if thats a compliment, a dig, or just a passing comment -
erm...thanks anyway :oD

Rob
Jul 19 '05 #18
here is an explanation of GUIDs (DCE UUIDs) and how they are constructed

http://www.opengroup.org/onlinepubs/9629399/apdxa.htm

Microsoft apparently uses a slightly different format (the described format
includes a flag to indicate this) but I have never been able to find a
description of the algorithm that they use. If anyone knows, I would
appreciate a link to it.

If reference to the original poster's needs. I would suggest that a license
key be shipped with the product but that when they register so that they can
get updates a more user-friendly id and password be generated. Associated
the license key with that user id in a database so that you can trace users
back to their license keys.

I would also use a license key format that included some information about
the product and the licensed user rather than a GUID. That way the
application can test the validity of the license during operation.

--
Mark Schupp
Head of Development
Integrity eLearning
www.ielearning.com
"Rob Meade" <ro********@NO-SPAM.kingswoodweb.net> wrote in message
news:jN*****************@news-text.cableinet.net...
"Aaron [SQL Server MVP]" wrote ...
Well, GUID stands for Globally Unique IDentifier, so you can do the math
from there...
Whilst not really wanting to go down this road, but feeling propelled to

say it....there must be an upper limit to the number of combinations though
surely - if I do the response.write GUID for example and then held F5 down
on my PC lets say for the rest of my life time I'm surely going to get a
duplicate eventually???

I'm unaware (and you might have to excuse my ignorance a little as Maths was never a strong subject for me) - of anything thats simply endless - apart
from space - but even that must have boundaries somewhere...(wooow - brain
is starting to melt a bit now)..
Keep this in mind, of course, if you are going to generate or

response.write
from ASP:
http://www.aspfaq.com/2358


Thanks for that, I didnt even know about the GUID ability in ASP so I've
learnt something new tonight :o)

Rob

Jul 19 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by vishal | last post: by
2 posts views Thread by Laphan | last post: by
11 posts views Thread by Zeya | last post: by
6 posts views Thread by comp.lang.php | last post: by
12 posts views Thread by Jim Michaels | last post: by
20 posts views Thread by jjmillertime | last post: by
8 posts views Thread by Marc | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.