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
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.
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.
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.
no subject
no subject
Gah, I hate this - what I know of coding languages is what I've taught myself, and therefore I only know what's come up as I've needed it, and when I hit something new to me, it amounts to miles of frustration over what other people view as basic stuff.
I'm pretty sure I can do this in PHP, even, but unfortunately the server I need to do this on doesn't have it.
ETA: And now I'm thinking I'm looking in the slightly wrong direction, and should be looking for ways to randomly sort arrays. Harrumph.
no subject
http://cassandra.wccs.edu/random.asp.txt
I think that might be what you need.
no subject
I shall look at that straghtaway.
no subject
I haven't worked with VB in ages, but I'll take a glance.
EDIT: Also, even though it tells you "where" the script screws up, it's always wrong and stupid. Just FYI.
no subject
* As my normal method for teaching myself something is to find other scripts, then monkey with them until I can see what changes cause what.
ETA: But it does have a terrifying consistency - I have tried about ten different scripts, all of which declare a type, all of which screw up exactly there.
no subject
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.)