Forum Moderators: open

Message Too Old, No Replies

Random generator not working!

looks good, but doesn't work...

         

nbozic

8:19 pm on Jul 12, 2004 (gmt 0)

10+ Year Member



Hi,

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

drbrain

8:34 pm on Jul 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RANDOMIZE() probably re-seeds your random number generator. You should only do that ONCE per application startup.

mattglet

9:05 pm on Jul 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or you could just use this one:

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

nbozic

9:20 pm on Jul 12, 2004 (gmt 0)

10+ Year Member



I'll try both of your suggestions.

Thanks guys

nbozic

9:28 pm on Jul 12, 2004 (gmt 0)

10+ Year Member



Strange... with both of your suggestions implemented, I still keep getting the same numbers. The only time the numbers change is when I make a change to the file and re-upload, or when enough time elapses (like a few hours).
Maybe the randomize() function is not working properly on my web host's server, and keeps prividing the same random seed... I have no clue.
I guess I'll have to contact my web host and ask them to fix the problem.
Any other ideas what the problem could be?

NB

mattglet

9:30 pm on Jul 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My example works for me on numerous sites of mine. Something is definitely going on somewhere. Your host might be a good place to start.

-Matt

nbozic

9:49 pm on Jul 12, 2004 (gmt 0)

10+ Year Member



Yes, I'll contact them right away.
By the way, thanks for "your version" of the function. It's definitely easier to maintain than the array one.

NB

drbrain

10:47 pm on Jul 12, 2004 (gmt 0)

uncle_bob

11:57 pm on Jul 12, 2004 (gmt 0)

10+ Year Member



Any reason why both code samples don't have the numbers 0 or 1?

duckhunter

2:19 am on Jul 13, 2004 (gmt 0)

10+ Year Member



No L's or O's either to avoid confusion

nbozic

2:28 am on Jul 13, 2004 (gmt 0)

10+ Year Member



Yes, it's to avoid confusion. I generate a GD image, and sometimes a 0 (zero) looks like the letter O, and so on... Since my GD image also has horizontal lines passing through it, I also have no "c" and no "e" (because when a horizontal line passes through "c" it make it look like an "e").

By the way, any other ideas why my random numbers stay the same? My web host support team did not help me much...
NB

mattglet

11:44 am on Jul 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How are you calling your function?

nbozic

5:41 pm on Jul 13, 2004 (gmt 0)

10+ Year Member



To call the function and output the result at the same time, I do this:
<%= randomcode(4)%>
...and the number stays the same for a while

ziggystardust

6:40 am on Jul 14, 2004 (gmt 0)

10+ Year Member



Checked your cache settings?

//ZS

nbozic

8:39 pm on Jul 14, 2004 (gmt 0)

10+ Year Member



How and where do I check the cache settings?

RainMaker

8:34 pm on Jul 15, 2004 (gmt 0)

10+ Year Member



Well nbozic, Are behind any type of firewall that would cache the web page(s) for a certain amount of time "to conserve bandwidth" at all like an ISA server. This could be potentially a web developers nightmare. Hopefully not?

Jimmy Turnip

10:11 am on Jul 16, 2004 (gmt 0)

10+ Year Member



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?

nbozic

4:30 pm on Jul 17, 2004 (gmt 0)

10+ Year Member



I just switched to another web host - the script works perfectly. Problem solved.
I guess my old web host cached the random seed on their server (didn't let the system timer update it automatically)... or something simply didn't work right.

NB

nbozic

4:38 pm on Jul 17, 2004 (gmt 0)

10+ Year Member



RainMaker, you might have been right about the server caching. After all, the web host mostly ignored me when I brought up the issue - they ALWAYS answered within 20 minutes, but that time they ignored 2 of my emails about the random seed generator. I guess they were simply not going to do anything about it.

Jimmy Turnip, I'll save your code in case I need it in the future.

Thanks.

NB