telophase: (gojyo screw you // yomigaere)
telophase ([personal profile] telophase) wrote2009-04-09 11:28 am
Entry tags:

aaaaaaaaaaaaaaaargh! VBScript

I've been wrestling with non-repeating random number* generator code all morning, and all the code samples I find don't work - every time they declare a variable AS something, the code dies. I can't figure out if it's because I'm doing something wrong, or if the version of VBScript I'm using doesn't support that.



For example, this code which I snagged somewhere, claims it shuffles an array of numbers from 1 to 10. But I get the error


Microsoft VBScript compilation error '800a0401'

Expected end of statement

/Surveys/2009-DeckTest.asp, line 16

Dim i As Long
------^


where it croaks at the As Long, which is the same thing with every other piece of code I've swiped - As Long, As Collection, As Integer. Aaaargh! It really doesn't help that I don't know enough about this stupid language.


Private Sub Form_Load()
Dim i As Long
Dim r As Long
Dim t As Long
Dim a(1 To 10) As Long
Randomize
'set up the array
For i = 1 To 10
a(i) = i
Next
'shuffle the array
For i = 1 To 10
r = Int(Rnd * 10) + 1 'get random value
'ensure r<>i
Do While r = i
r = Int(Rnd * 10) + 1
Loop
'swap the array members
t = a(i)
a(i) = a(r)
a(r) = t
Next
'output
For i = 1 To 10
Debug.Print a(i)
Next
End Sub


Context is that I am writing a survey app which, when someone agrees to answer a question for us, will grab the active questions from the database and shuffle them, then present the first one. After the patron answers that, it asks if they'd be willing to answer another. If so, then it grabs the next one in the array. Lather, rise, repeat. At the end of the array, it gives a polite message that there are no more questions to answer at this time. I need for questions not to repeat, and for them to be presented (pseudo-)randomly, so that if we're asking similar questions, we don't get biased answers because certain ones always appear first (if I'm explaining that correctly).








--
* Yes, I know, non-repeating makes it technically not random.

[identity profile] ukoku.livejournal.com 2009-04-09 06:10 pm (UTC)(link)
Nah, I'm interested now, so I'm going to fiddle. Lol.

One thing I think I've found, is that you need a line that says "Start:" before the "Randomize", but I'm not sure if that's it.

(and now it's tellin gme I'm out of stack space....)

(Good luck with the other script, though.)