Why wont this Randomize? (Classic VB Script ASP)

  • Thread starter Badass Scotsman
  • 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.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top