Advertisement

Random Questions

Started by September 23, 2002 07:37 PM
3 comments, last by Surg AKA Kunark 22 years, 3 months ago
Would anyone know of a way to get questions to go in a random order, without having to like re-write all the questions in different orders than use an if elseif statement to say if random number is equal to 1 or whatever.
~~Johnathan~~LTM my new fad, know it, use it, love it, LAUGHING TO MYSELF
Hmm... I'm not sure I get your question, but I'll give it a shot.

Are you trying to find an alternative to this?


      int num = rand()%3;if (num == 0){    cout << "Question 1" << endl;} else if (num == 1){    cout << "Question 2" << endl;} else if (num == 2){    cout << "Question 3" << endl;}  


In that case, I'd suggest maybe something like this:


  char questions[3][100] = {"Question 1",                          "Question 2",                          "Question 3"};int num = rand()%3;cout << questions[num] << endl;  


Answers could be handled in a similar manner, that is to say in an array of answers, listed in corresponding order. Something like:

if (input == answers[num]){}

Hope I'm answering your question...

EDIT- Don't you hate it when you accidentally do = instead of ==?

-Arek the Absolute

[edited by - Arek the Absolute on September 23, 2002 9:35:55 PM]
-Arek the Absolute"The full quartet is pirates, ninjas, zombies, and robots. Create a game which involves all four, and you risk being blinded by the sheer level of coolness involved." - Superpig
Advertisement
Thanx for the reply, also yes i do hate it when i go = instead of ==, i did that in an entire program once helping someone in school and it took us forever to firgure out why it wasn''t working.
~~Johnathan~~LTM my new fad, know it, use it, love it, LAUGHING TO MYSELF
quote: Original post by Arek the Absolute
Don't you hate it when you accidentally do = instead of ==?

-Arek the Absolute



That's why I always do something like this:


    if( 0 == num ) {}else if( 1 == num ) {}else if( 2 == num ) {}     


If you type in the equalities backwards, 0 = num will result in a compiler error.
It takes a while to get used to doing this, but it saves a whole lot of time in debugging, and makes the compiler _work_ for you...

[edited by - doomx on September 24, 2002 12:07:47 PM]
There are 10 kinds of people in the world: those who get binary, and those who don't...
quote: Original post by Surg AKA Kunark
Would anyone know of a way to get questions to go in a random order, without having to like re-write all the questions in different orders than use an if elseif statement to say if random number is equal to 1 or whatever.

What language are you using? Since you didn't specify, I'm going to assume C++. What you should probably do is store all your questions and answers in STL collections. Then you can perform a random_shuffle on the collection and simply pop questions from the collection. Here's an example of how you would do a random_shuffle:


    #include <algorithm>#include <functional>#include <iostream>#include <iterator>#include <cstdlib>#include <ctime>struct rand_functor : std::unary_function<int,int>{	result_type operator()(argument_type N) const	{  		return N * (std::rand() / (RAND_MAX + 1.0));	}};int main(){	const int numbers_size = 10;	int numbers[numbers_size] = {1,2,3,4,5,6,7,8,9,10};	std::srand(std::time(NULL));	std::random_shuffle(numbers, numbers+numbers_size, rand_functor());	std::copy(numbers, numbers+numbers_size,  		std::ostream_iterator<int>(std::cout," ")	);}    


[edited by - SabreMan on September 24, 2002 12:18:11 PM]

This topic is closed to new replies.

Advertisement