468,140 Members | 1,248 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,140 developers. It's quick & easy.

Why wont this Randomize? (Classic VB Script ASP)

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
1 3388
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.

Similar topics

3 posts views Thread by Gaffer | last post: by
4 posts views Thread by Mr. x | last post: by
5 posts views Thread by Velvet | last post: by
6 posts views Thread by mrtaka79 | last post: by
5 posts views Thread by =?Utf-8?B?SmltIFJvZGdlcnM=?= | last post: by
27 posts views Thread by didacticone | last post: by
1 post views Thread by gcdp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.