By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,743 Members | 1,028 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,743 IT Pros & Developers. It's quick & easy.

Why wont this Randomize? (Classic VB Script ASP)

P: n/a
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,numberofcombin ations)
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.
May 5 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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
Badass Scotsman wrote:
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,numberofcombin ations)
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.

May 5 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.