ASP & Rnd

They have: 330 posts

Joined: Apr 2000

I am creating a web-based quiz which will have specific categories. Each category will have 20-30 questions associated with it's categoryID. I would like to use the Rnd function to display 10 random questions pulling from the 20-30 available questions.

The problem - I have never used Rnd so I am lost. How do I make sure it doesn't randomly select an already displayed question?

Thanks for any help.

detox's picture

They have: 571 posts

Joined: Feb 2001

simply tag the questions that you get from the random function so that if the question is picked again it will be disregarded.

I'll have a go at something for you.....

detox's picture

They have: 571 posts

Joined: Feb 2001

I have it done.

I have a db with 40 questions in it, and the corresponding answers. I have built a quiz with 10 questions, and put it in the form of a multiple choice quiz, if you want to change it you can. I'll zip it up (db and all) and put it on my site for you...

will get back to you with a link soon.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

wow... quick

If you want random numbers that don't repeat, use a loop and an array. In the loop, grab a random number and check to see if it's in the array. If so, loop and try again. Once you've got a number that is not in the array, exit the loop. Add the number to the array, and use it where ever you needed it for (DB query, etc.).

Did that make any sense? Confused

Mark Hensler
If there is no answer on Google, then there is no question.

detox's picture

They have: 571 posts

Joined: Feb 2001

OK,

here we go. My hosts server was down for a bit. You can download it all here http://detoxdezigns.com/TWF/quiz.zip

The db and asp page are all there in a zip file. The code is commented quite heavily but if you have any problems give me a pm and well work through them. The way the questions and answers are displayed can be changed really easily.

They have: 330 posts

Joined: Apr 2000

Thank you very much for the responses. I have also been working on another way of making this happen because I was not good enough at coding to do what you were able to do detox. My way is working, let me explain what I did.

I have an array being built from the DB using GetRows. Once the data is dumped to the array I run a random script which will select one record from the array. That number is then saved to a new array and then moved to the last record in the array...

Sub RemoveQuestion(intRecID)
QuestionArray(1,intRecID) = QuestionArray(UBound(QuestionArray,2))
Redim Preserve QuestionArray(UBound(QuestionArray,2) - 1)
End Sub
'
I then have the same array with 1 less record which is the one I just chose. While looping through that it removes each record requested.

The reason I sent it to another array is so I can pass that through the querystring or anywhere to reference the order later.

Does this make sense to do it this way? Any suggestions?

Peter J. Boettcher's picture

They have: 812 posts

Joined: Feb 2000

I was going to suggest using two arrays. Good idea!

Detox, thanks for going the extra mile, great work!

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.