Wrox Home  
Search P2P Archive for: Go

  Return to Index  

asp_web_howto thread: Generating Unique Random Activation Code


Message #1 by "Thamir Abdullatif" <thamir@w...> on Sat, 20 Apr 2002 10:51:51
Greetings,

I want to generate unique random activation codes in alphanumeric. This 
activation code is required to activate an account and discarded 
afterward. The algorithm should guarantee the uniquness of the generarated 
activation codes and should not clash with old ones. Any help?

Regards,
Thamir
Message #2 by "Tony DiNucci" <tony@m...> on Mon, 22 Apr 2002 02:17:58
the following function cannot guarantee a unique code on its 
own.........but you can compair it to existing fields in your db and that 
would work.

to call it just do something like the following:

strActivateCode = makeCode(6) ' where 6 is the number of characters

********************************************************************

' builds a random code that will later be e-mailed to the new user
function makeCode(byVal maxLen)
dim strNewCode
dim whatsNext, upper, lower, intCounter
randomize

for intCounter = 1 to maxLen
    whatsNext = int((1 - 0 + 1) * rnd + 0)
if whatsNext = 0 then
'character
  upper = 90
  lower = 65
else
  upper = 57
  lower = 48
end if
  strNewCode = strNewCode & Chr(Int((upper - lower + 1) * rnd + lower))
next
  makeCode = strNewCode
end function
Message #3 by Oleg Kapeljushnik <c-oleg.kapeljushnik@w...> on Sun, 21 Apr 2002 21:41:28 -0400
I don't know the exact algorithm but I think 
That you can create unique number based on date and time data.
Cause date and time together always will be unique.
If you have few users in the same time you can add milliseconds into it
Or do add some another user identification.

Oleg.

-----Original Message-----
From: Thamir Abdullatif [mailto:thamir@w...] 
Sent: Saturday, April 20, 2002 6:52 AM
To: ASP Web HowTo
Subject: [asp_web_howto] Generating Unique Random Activation Code

Greetings,

I want to generate unique random activation codes in alphanumeric. This 
activation code is required to activate an account and discarded 
afterward. The algorithm should guarantee the uniquness of the
generarated 
activation codes and should not clash with old ones. Any help?

Regards,
Thamir

---

Improve your web design skills with these new books from Glasshaus.

Usable Web Menus
http://www.amazon.com/exec/obidos/ASIN/1904151027/ref=nosim/theprogramme
r-20
Constructing Accessible Web Sites
http://www.amazon.com/exec/obidos/ASIN/1904151000/ref=nosim/theprogramme
r-20
Practical JavaScript for the Usable Web
http://www.amazon.com/exec/obidos/ASIN/1904151051/ref=nosim/theprogramme
r-20

Message #4 by "Ken Schaefer" <ken@a...> on Mon, 22 Apr 2002 12:32:17 +1000
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From: "Thamir Abdullatif" <thamir@w...>
Subject: [asp_web_howto] Generating Unique Random Activation Code


: I want to generate unique random activation codes in alphanumeric. This
: activation code is required to activate an account and discarded
: afterward. The algorithm should guarantee the uniquness of the generarated
: activation codes and should not clash with old ones. Any help?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

<%
Set TypeLib = Server.CreateObject("Scriptlet.TypeLib")
intGUID = TypeLib.GUID
Set TypeLib = Nothing
%>

The GUID is a null terminated string, so you might want to chop the last
character off the end if you want to append something.

Cheers
Ken

Message #5 by Joe Ingle <Joe@k...> on Mon, 22 Apr 2002 08:30:44 +0100
Bit late on the uptake on this one, so you probably already have a solution
for this. I normally create a unique ID by first creating a using randonize
to get a random, x digit, string (this could be alpha or alpha numeric),
then using the DatDiff function to get the time in seconds from a given
date. i.e 08/04/72. Then join the two together. i.e.:

<%
Function GenKeyOne(digits)

Dim GenKeyTwo
Dim char_array(35)
Dim RandCode

char_array(0) 	= "0"
char_array(1) 	= "1"
char_array(2) 	= "2"
char_array(3) 	= "3"
char_array(4) 	= "4"
char_array(5) 	= "5"
char_array(6) 	= "6"
char_array(7) 	= "7"
char_array(8) 	= "8"
char_array(9) 	= "9"
char_array(10) 	= "A"
char_array(11) 	= "B"
char_array(12) 	= "C"
char_array(13) 	= "D"
char_array(14) 	= "E"
char_array(15) 	= "F"
char_array(16) 	= "G"
char_array(17) 	= "H"
char_array(18) 	= "I"
char_array(19) 	= "J"
char_array(20) 	= "K"
char_array(21) 	= "L"
char_array(22) 	= "M"
char_array(23) 	= "N"
char_array(24) 	= "O"
char_array(25) 	= "P"
char_array(26) 	= "Q"
char_array(27) 	= "R"
char_array(28) 	= "S"
char_array(29) 	= "T"
char_array(30) 	= "U"
char_array(31) 	= "V"
char_array(32) 	= "W"
char_array(33) 	= "X"
char_array(34) 	= "Y"
char_array(35) 	= "Z"

randomize

do while len(output) < digits
num = char_array(Int((35 - 0 + 1) * Rnd + 0))
output = output + num
loop

GenKeyOne = output
End Function

GenKeyTwo = DateDiff("s","08/04/72",now()) 

RandCode = ""&GenKeyOne(3)&""&GenKeyTwo&""


response.write(""&RandCode&"")
%>

Hope this works for you.

Joe

-----Original Message-----
From: Thamir Abdullatif [mailto:thamir@w...]
Sent: Saturday, April 20, 2002 11:52 AM
To: ASP Web HowTo
Subject: [asp_web_howto] Generating Unique Random Activation Code


Greetings,

I want to generate unique random activation codes in alphanumeric. This 
activation code is required to activate an account and discarded 
afterward. The algorithm should guarantee the uniquness of the generarated 
activation codes and should not clash with old ones. Any help?

Regards,
Thamir

---

Improve your web design skills with these new books from Glasshaus.

Usable Web Menus
http://www.amazon.com/exec/obidos/ASIN/1904151027/ref=nosim/theprogramme
r-20
Constructing Accessible Web Sites
http://www.amazon.com/exec/obidos/ASIN/1904151000/ref=nosim/theprogramme
r-20
Practical JavaScript for the Usable Web
http://www.amazon.com/exec/obidos/ASIN/1904151051/ref=nosim/theprogramme
r-20
Message #6 by william.sze@s... on Mon, 22 Apr 2002 09:19:53 -0400
Hi Tony,

You mind tell me what is the logic/idea behind

Int((upper - lower + 1) * rnd + lower)

or for the whole function

thanks
William



                                                                                                                      
                    "Tony DiNucci"                                                                                    
                    <tony@m...       To:     "ASP Web HowTo" <asp_web_howto@p...>                      
                    dia.co.uk>              cc:                                                                       
                                            Subject:     [asp_web_howto] Re: Generating Unique Random Activation Code 
                    04/21/02 10:17 PM                                                                                 
                    Please respond to                                                                                 
                    "ASP Web HowTo"                                                                                   
                                                                                                                      
                                                                                                                      




the following function cannot guarantee a unique code on its
own.........but you can compair it to existing fields in your db and that
would work.

to call it just do something like the following:

strActivateCode = makeCode(6) ' where 6 is the number of characters

********************************************************************

' builds a random code that will later be e-mailed to the new user
function makeCode(byVal maxLen)
dim strNewCode
dim whatsNext, upper, lower, intCounter
randomize

for intCounter = 1 to maxLen
    whatsNext = int((1 - 0 + 1) * rnd + 0)
if whatsNext = 0 then
'character
  upper = 90
  lower = 65
else
  upper = 57
  lower = 48
end if
  strNewCode = strNewCode & Chr(Int((upper - lower + 1) * rnd + lower))
next
  makeCode = strNewCode
end function

---

Improve your web design skills with these new books from Glasshaus.

Usable Web Menus
http://www.amazon.com/exec/obidos/ASIN/1904151027/ref=nosim/theprogramme
r-20
Constructing Accessible Web Sites
http://www.amazon.com/exec/obidos/ASIN/1904151000/ref=nosim/theprogramme
r-20
Practical JavaScript for the Usable Web
http://www.amazon.com/exec/obidos/ASIN/1904151051/ref=nosim/theprogramme
r-20




Message #7 by "Tony DiNucci" <tony@m...> on Mon, 22 Apr 2002 19:50:43
hi there,
as this function is producing either numbers between 0-9 and uppercase 
letters A-Z this need to be done to stop unwanted characters slipping 
through.

' this creates either a 1 or 0
whatsNext = int((1 - 0 + 1) * rnd + 0)

upper = 90 ' = ASCII Z
lower = 65 ' = ASCII A

upper = 57 ' = ASCII 9
lower = 48 ' = ASCII 0

strNewCode = strNewCode & Chr(Int((upper - lower + 1) * rnd + lower))

(upper - lower + 1) will depend on if a 0 or 1 was generated at the start.
if 0 was generated then you have: 
(90 - 65 +1) = 26
as you know 26 isn't an uppercase letter, or even a number.  we then 
multiply it by a random fractional number, which actually lowers the 
number even further and add the lower characters value to it.
for eg:
(90 - 65 +1) * 0.3 + lower = 72.8

since int and chr will take it as 73 this will produce "I"
the same goes for the numbers........doing it this you wont get any ASCII 
codes outside the upper and lower ranges.

hope that clears it up for you.

> 
Hi Tony,

You mind tell me what is the logic/idea behind

Int((upper - lower + 1) * rnd + lower)

or for the whole function

thanks
William



                                                                           
                                           
                    "Tony 
DiNucci"                                                                   
                 
                    <tony@m...       To:     "ASP Web HowTo" 
<asp_web_howto@p...>                      
                    dia.co.uk>              
cc:                                                                       
                                            Subject:     [asp_web_howto] 
Re: Generating Unique Random Activation Code 
                    04/21/02 10:17 
PM                                                                         
        
                    Please respond 
to                                                                         
        
                    "ASP Web 
HowTo"                                                                     
              
                                                                           
                                           
                                                                           
                                           




the following function cannot guarantee a unique code on its
own.........but you can compair it to existing fields in your db and that
would work.

to call it just do something like the following:

strActivateCode = makeCode(6) ' where 6 is the number of characters

********************************************************************

' builds a random code that will later be e-mailed to the new user
function makeCode(byVal maxLen)
dim strNewCode
dim whatsNext, upper, lower, intCounter
randomize

for intCounter = 1 to maxLen
    whatsNext = int((1 - 0 + 1) * rnd + 0)
if whatsNext = 0 then
'character
  upper = 90
  lower = 65
else
  upper = 57
  lower = 48
end if
  strNewCode = strNewCode & Chr(Int((upper - lower + 1) * rnd + lower))
next
  makeCode = strNewCode
end function

---

Improve your web design skills with these new books from Glasshaus.

Usable Web Menus
http://www.amazon.com/exec/obidos/ASIN/1904151027/ref=nosim/theprogramme
r-20
Constructing Accessible Web Sites
http://www.amazon.com/exec/obidos/ASIN/1904151000/ref=nosim/theprogramme
r-20
Practical JavaScript for the Usable Web
http://www.amazon.com/exec/obidos/ASIN/1904151051/ref=nosim/theprogramme
r-20




Message #8 by william.sze@s... on Mon, 22 Apr 2002 15:12:39 -0400
Great. Thanks for your detail reply.

William


                                                                                                                      
                    "Tony DiNucci"                                                                                    
                    <tony@m...       To:     "ASP Web HowTo" <asp_web_howto@p...>                      
                    dia.co.uk>              cc:                                                                       
                                            Subject:     [asp_web_howto] Re: Generating Unique Random Activation Code 
                    04/22/02 03:50 PM                                                                                 
                    Please respond to                                                                                 
                    "ASP Web HowTo"                                                                                   
                                                                                                                      
                                                                                                                      




hi there,
as this function is producing either numbers between 0-9 and uppercase
letters A-Z this need to be done to stop unwanted characters slipping
through.

' this creates either a 1 or 0
whatsNext = int((1 - 0 + 1) * rnd + 0)

upper = 90 ' = ASCII Z
lower = 65 ' = ASCII A

upper = 57 ' = ASCII 9
lower = 48 ' = ASCII 0

strNewCode = strNewCode & Chr(Int((upper - lower + 1) * rnd + lower))

(upper - lower + 1) will depend on if a 0 or 1 was generated at the start.
if 0 was generated then you have:
(90 - 65 +1) = 26
as you know 26 isn't an uppercase letter, or even a number.  we then
multiply it by a random fractional number, which actually lowers the
number even further and add the lower characters value to it.
for eg:
(90 - 65 +1) * 0.3 + lower = 72.8

since int and chr will take it as 73 this will produce "I"
the same goes for the numbers........doing it this you wont get any ASCII
codes outside the upper and lower ranges.

hope that clears it up for you.

>
Hi Tony,

You mind tell me what is the logic/idea behind

Int((upper - lower + 1) * rnd + lower)

or for the whole function

thanks
William





                    "Tony
DiNucci"

                    <tony@m...       To:     "ASP Web HowTo"
<asp_web_howto@p...>
                    dia.co.uk>
cc:
                                            Subject:     [asp_web_howto]
Re: Generating Unique Random Activation Code
                    04/21/02 10:17
PM

                    Please respond
to

                    "ASP Web
HowTo"









the following function cannot guarantee a unique code on its
own.........but you can compair it to existing fields in your db and that
would work.

to call it just do something like the following:

strActivateCode = makeCode(6) ' where 6 is the number of characters

********************************************************************

' builds a random code that will later be e-mailed to the new user
function makeCode(byVal maxLen)
dim strNewCode
dim whatsNext, upper, lower, intCounter
randomize

for intCounter = 1 to maxLen
    whatsNext = int((1 - 0 + 1) * rnd + 0)
if whatsNext = 0 then
'character
  upper = 90
  lower = 65
else
  upper = 57
  lower = 48
end if
  strNewCode = strNewCode & Chr(Int((upper - lower + 1) * rnd + lower))
next
  makeCode = strNewCode
end function

---

Improve your web design skills with these new books from Glasshaus.

Usable Web Menus
http://www.amazon.com/exec/obidos/ASIN/1904151027/ref=nosim/theprogramme
r-20
Constructing Accessible Web Sites
http://www.amazon.com/exec/obidos/ASIN/1904151000/ref=nosim/theprogramme
r-20
Practical JavaScript for the Usable Web
http://www.amazon.com/exec/obidos/ASIN/1904151051/ref=nosim/theprogramme
r-20





---

Improve your web design skills with these new books from Glasshaus.

Usable Web Menus
http://www.amazon.com/exec/obidos/ASIN/1904151027/ref=nosim/theprogramme
r-20
Constructing Accessible Web Sites
http://www.amazon.com/exec/obidos/ASIN/1904151000/ref=nosim/theprogramme
r-20
Practical JavaScript for the Usable Web
http://www.amazon.com/exec/obidos/ASIN/1904151051/ref=nosim/theprogramme
r-20





  Return to Index