View Full Version : Random Password
Crash1hd
05-22-2003, 08:47 AM
Ok the following code creates a random set of 8 letters, what I want to do is add numbers in the mix
<%
' Declare Variables
Dim intCounter, intDecimal, strPassword
' Create a 8-Character random password
For intCounter = 1 To 8
' Generate a random number between 65 and 90.
Randomize
'intDecimal = Int((9 * Rnd) + 1) + 47
'intDecimal = Int((26 * Rnd) + 1) + 64
' Create random Password String
strPassword = strPassword & LCase(Chr(intDecimal))
Next
' Output the random password
Response.Write "Random Password = " & strPassword
%>
The red is what I would use if I wanted it to be all numbers!
The blue is what I would use if I wanted it to be all Letters
cause the ascii code indicates that numbers are in the range of 48 and 57 and letters are in the range of 65 and 90
I am aware that the numbers 97 to 122 are lowercase letters so I wouldnt have to use LCase to make them lower case I also wouldnt mind If i could mix lower case and upper case with numbers all three :) not to be gready or nothing lol
but how do I mix the red and blue
david7777
05-22-2003, 09:35 AM
Instead of limiting the ascii character to only 47-57, or 97-122, use everything that you may want in the password. ie: use 47-57 AND 97-122 AND any other you may want like uppercase letters...
So maybe you would have a template for your passwrod like:
ss#C##CC
Where C=Uppercase letter, #=number, s=lowercase letter
So the first two loops you must generate a random number between 97 and 122.
The 3rd loop get a random number between 47 and 57.
The fourth loop generate a random number for an uppercase letter and so on.
So you will keep the loop like you have it, but add if or case statements to determine what to do in certain loop numbers...
Than add them all together to get a password. ie: You may get something like this: ge7R02WM
Crash1hd
05-22-2003, 07:07 PM
Ok but how would that look in code?
david7777
05-23-2003, 08:02 AM
Ok, assuming you want a password with the format of my last post (ss#C##CC), try this:
<%
' Declare Variables
Dim intCounter, intDecimal, strPassword
' Create a 8-Character random password
For intCounter = 1 To 8
Randomize
' Generate a random lower case letter for first 2 loops
if intCounter = 1 or intCounter = 2 then
intDecimal = Int((26 * Rnd) + 1) + 64
' Generate a single random integer if in loop 3, 5 or 6
elseif intCounter = 3 or intCounter = 5 or intCounter = 6 then
intDecimal = Int((9 * Rnd) + 1) + 47
' Generate a random upper case letter if in loop 4, 7 or 8
elseif intCounter = 4 or intCounter =7 or intCounter = 8 then
' Change this to indicate for the ascii code for uppercase letters (Im not sure what it is??)
intDecimal = Int((9 * Rnd) + 1) + 47
end if
' Create random Password String
strPassword = strPassword & LCase(Chr(intDecimal))
Next
' Output the random password
Response.Write "Random Password = " & strPassword
%>
Make sense?
So obviously you can play around with that and change the template of your password...
Hope this helped :)
Spudhead
05-23-2003, 09:48 AM
Or... have some subroutines. One generates a random integer betwen 0 and 9, and one generates a random letter. Loop through the length you want your password string to be - say, 8 times, generating a random number. If that number is greater than 5, generate a random number in that position. If it's less than 5, generate a random letter for it.
Crash1hd
05-23-2003, 09:51 AM
Thanks david7777 I wasnt even thinking about my if thens lol I dont know why!!
HEHE Must have been one to many Jolt colas lol Sleep is good!
whammy
05-24-2003, 12:48 AM
Well you can also use this in JavaScript server-side (or convert it to VBScript, but why bother when you can use both?):
<script language="javascript" runat="server">
function setKey(){
var returnVal = "";
var keyLen = Math.round(Math.random() * 16) + 16;
for(var i = 0; i < keyLen; i ++) {
returnVal += Math.floor(Math.random() * 10);
}
return returnVal;
}
</script>
An example of how to use it in VBScript (although I've seen a lot of problems with people trying to use return values from VBScript, it's NOT a problem the other way around!):
Session("ekey") = setKey()
whammy
05-24-2003, 12:48 AM
Obviously you could use characters instead of just numbers, that's just an example...
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.