Forum Moderators: open
I just finished creating my random string generator. Everything looks fine, however, it always gives me the same random number when I call the function (even when I refresh the asp page). The only exception is when a lot of time goes by, then it would give me a different number - for example, tomorrow it might give me a different number. The current output is "fiid".
Here is the function, and it DOES have the "randomize" statement:
FUNCTION randomcode(codelength)
'Array of characters being used for the random code
codearray = Array("a","b","d","f","h","i","j","k","m","n","p","r","s","t","u","v","w","x","y","z", _
"2","3","4","5","6","7","8","9", _
"A","B","C","D","E","F","J","K","M","N","P","Q","R","S","T","U","V","W","X","Y","Z")
'Generates one random character until it reaches code length
FOR x = 1 TO codelength
RANDOMIZE()
'Gets a random number from the array
thiscode = (Int(((Len("codearray") - 1) * Rnd()) + 1))
'building the code one by one character...
totalcode = totalcode & codearray(thiscode)
NEXT
randomcode = totalcode 'Random code that is returned
END FUNCTION
Any ideas?
Thanks,
NB
function RandomString(nLength)
Randomize()
dim pattern, dif, i, strSID
pattern = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghijklmnopqrstuvwxyz"
dif = len(pattern) - 1
for i = 1 to nLength
strSID = strSID & mid(pattern,round((rnd()*dif)+1),1)
next
RandomString = strSID
end function
-Matt
NB
By the way, any other ideas why my random numbers stay the same? My web host support team did not help me much...
NB
By the way, any other ideas why my random numbers stay the same?
Don't know why, but you could do some math and multiply the clock by the length of the array and then divide by the random number generated to give you a more 'random' number.
totalcode = Int(Int(Replace(Replace(Replace(Now(), ":", ""), "/", ""), " ", "")) * (Len("codearray") - 1) / Rnd())
or something?
Jimmy Turnip, I'll save your code in case I need it in the future.
Thanks.
NB