ASP(VBScript) trouble with arrays
by Andrej Kostresevic <temporalassassin(at)yahoo.com>
|
Date: |
Fri, 11 May 2001 14:01:19 -0700 (PDT) |
To: |
hwg-languages(at)hwg.org |
In-Reply-To: |
fredonia |
|
todo: View
Thread,
Original
|
|
I have this interesting little problem.
This test page is supposed to simply write out 20 random unique numbers.
It is timing out, and when it does execute, there are repeated numbers,
although the used() and add() functions should be taking care of them.
Here is the code:
<%
Randomize()
Dim NumWords
NumWords = cint(20)
Dim arrUsed()
ReDim arrUsed(NumWords)
Function used(lNum)
If arrUsed(iNum) = 1 Then
bused = true
Else
bused = false
End If
used = bused
End Function
Function Add(lNum)
arrUsed(lNum) = 1
End Function
cnt = 0
While cnt < 20 ' do 20 random numbers
lIndex = cint(rnd()*(NumWords-1))'get a num between 0 and NumWords
bUsed = used(lIndex)
While bUsed 'while the number has already been used
lIndex = cint(rnd()*NumWords-1)'get a new random number
bUsed = used(lIndex)'check whether it has been used
wend
response.write lIndex &"<br>" 'spit it out
Add(lIndex)'flag it as used
cnt = cnt +1' next
wend
%>
The page that is going to use it is just an experiment, but I just want to
know what it is that I am doing wrong, so I don't do it on an important
application some day. :)
TIA,
Andrej
__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/
HWG: hwg-languages mailing list archives,
maintained by Webmasters @ IWA