Why wont this Randomize? (Classic VB Script ASP)

  • Thread starter Thread starter Badass Scotsman
  • Start date Start date
B

Badass Scotsman

Hello,

This code is supposed to generate a random string each run, however I have
had it live on a few sites, and it seems to create repeat strings all over
the place.

-------------------------------------------------------------
<%
FUNCTION GetRandomCode(randomcode,codelength,numberofcombinations)
codecharacters = 35
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")
FOR x = 1 TO codelength
RANDOMIZE
thiscode = (Int(((codecharacters - 1) * Rnd) + 1))
totalcode = totalcode & codearray(thiscode)
IF numberofcombinations = "" THEN numberofcombinations = 1
numberofcombinations = numberofcombinations * codecharacters
NEXT
randomcode = totalcode
END FUNCTION
CALL GetRandomCode(randomcode,35,numberofcombinations)
%>
-------------------------------------------------------------


Any you guys care to help? :)

Thanks in advance,

Badass.
 
Put the Randomize outside the loop. The Randomize uses the system time
to seed the random generator, if you call it too often the system time
will not have changed between the calls, so it will seed the random
generator with the same value.

I also noticed that you are not using all characters in the array. Have
you noticed that the generated codes never contains the character "a"?

I have a function in my code archive that creates random codes. You
might want to have a look:

http://www.guffa.com/Programming_archive.asp?id=25
 
Back
Top