MGinternet, Web Design Ireland
 
Welcome Guest, Please Login Here
 
RESOURCES
MY ACCOUNT
Home > Resources > ASP Source Code > ASP Random Code Generator
ASP Source Code - ASP CMS - Irish Web Hosting - Web Design Ireland - MGinternet

Rated:
by Aspin.com users
What do you think?
This little function generates a random code everytime you call it. Specify the length of the code when calling and get a report about the number of combinations of the code length there is. Handy for order numbers, random passwords, and so on! Enjoy!

Installation: Follow the instructions commented in the source code.

ASP Part

<%
'*******************************************************
'Background to this script
'*******************************************************
'
'Date: 10 March 2003
'Company: MGinternet, Meath, Ireland
'E-Mail: scripts@mginternet.com
'Web Site: http://www.mginternet.com
'
'**************************************************************
'This function generates a random code based on the code length
'specified when calling the function. The random code is
'returned under the variable "randomcode".
'**************************************************************
FUNCTION GetRandomCode(randomcode,codelength,numberofcombinations)

'Number of characters in the array below
codecharacters = 35

'Array of characters being used for the random code
codearray = Array("a","b","c","d","e","f","g","h","i","j","k","l", _
"m","n","o","p","q","r","s","t","u","v","w","x", _
"y","z","1","2","3","4","5","6","7","8","9")

'Generates one random character until it reaches code length
FOR x = 1 TO codelength
RANDOMIZE
'Gets a random number based on the value in the codecharacters variable
thiscode = (Int(((codecharacters - 1) * Rnd) + 1))

'builds the code on top of itself until complete by selecting the
'character from the array based on the random number generated above
totalcode = totalcode & codearray(thiscode)

'This is only an extra thing to tell you how many combinations
'there are for the code length specified. You may get scientific
'notation to describe the length so, just take it that is damn near
'impossible to guess the code.
IF numberofcombinations = "" THEN numberofcombinations = 1
numberofcombinations = numberofcombinations * codecharacters

NEXT

'Random code that is returned after codelength has been fulfilled
randomcode = totalcode

END FUNCTION

'The following line calls (runs) the above script. First variable can be left
'as is but you can change the value below to any number to generate a code of
'that length. 3 returns a three character random code, 9 returns a nine
'character random code... and so on!
CALL GetRandomCode(randomcode,9,numberofcombinations)

'You can now use the code for whatever purpose e.g. order number, random password,
'unique id, etc, etc.
Response.Write(randomcode)

'This is only an extra thing to tell you how many combinations there are to the code length.
Response.Write("<br><br>Total combinations for this code length: " & FormatNumber(numberofcombinations,0))
%>