Advertisement

Question about asking questions in a random order with C++

Started by February 05, 2003 11:04 PM
6 comments, last by Digigamer15 21 years, 9 months ago
I''m trying to make a program where I put in x amount of questions into the source code and when the program is run it will ask those questions in a random order but I don''t know how to make it random. I know how to make it ask the questions I put in the source code but I don''t know how to make the program ask them in a different order every time. Thanks ps. please provide explenations so that I will understand how to do what you are tell me. So don''t just say use #include time.h that won''t help me at all
-----------------------------AIM: Trebor DoDHompage: Thinking Digitally: My Web Blog
*bump*

-----------------------------
Programming is confussing!
AIM: Trebor DoD
-----------------------------AIM: Trebor DoDHompage: Thinking Digitally: My Web Blog
Advertisement
Just use the rand function. Something like

  srand(time(NULL));int result = rand() % 3;switch(result){ case 0:  AskQuestion1;  break;  case 1:  AskQuestion2;  break; case 2:  AskQuestion3;  break;}  

just ugly pseudo-code, but you get the idea

  #include <algorithm>#include <iostream>#include <ctime>#include <cstdlib>int main() {  using namespace std;  int order[5] = {0, 1, 2, 3, 4};    srand(time(0));    for(int p = 0; p < 5; ++p)   swap(order[p], order[rand()%5]);  for(int p = 0; p < 5; ++p)   switch(order[p])    {     case 0:      cout << "Why are there flotation devices under plane seats instead of parachutes?" << endl;     break;          case 1:      cout << "Why are cigarettes sold in gas stations when smoking is prohibited there?" << endl;     break;         case 2:      cout << "Why do we drive on parkways and park on driveways?" << endl;     break;          case 3:      cout << "If someone with multiple personalities threatens suicide, is it considered a hostage situation?" << endl;     break;          case 4:      cout << "Why is it that when you transport something by car, it''s called a shipment, but when you transport something by ship, it''s called cargo?" << endl;     break;    }  return 0; }  
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
alright thanks, smart idiot. ill try that out tonight.

-----------------------------
Programming is confussing!
AIM: Trebor DoD
-----------------------------AIM: Trebor DoDHompage: Thinking Digitally: My Web Blog
Hey everyone !
I have a related question:
I have been doing some litlle progs with rand() and srand() recently,and i use a pretty simple code ie: srand based on time, and just a ...cout<< variable; ...after the random value from rand() has been asigned to and the problem i have is that the numbers i am getting are not really random ( i know, rand is just an algorithm but srand should change that...) : it seems like the result i''m getting is just an random INCREMENING (1..5..7..12etc..). I have found some crappy workarounds, but i still don''t get the logic behind it : could any one help please ?
Just trying to get it done !http://ckaosdevblog.blogspot.com/
Advertisement
Are you calling srand every time before you call rand? Call it ONCE, at the beginning of your program. That ''seeds'' the random number generator.

Don''t listen to me. I''ve had too much coffee.
hey thanks , i thought it was ok if i called it at the beginning of my function (outside the while loop that displays the numbers), but putting it in main() instead seems to have solved the problem...
Just trying to get it done !http://ckaosdevblog.blogspot.com/

This topic is closed to new replies.

Advertisement