Generation of random possible answers (multiple choice style)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
char ans2;

    for(int i = 0; i < 10; i++)
    {

          cout << p2Q[i].getQuestion() << endl;

        for(int i = 0; i < 4; i++)
    {

        int randIndex = rand() %4; // generates a random number between 0 and 3
        cout << q1Possible[randIndex] << endl;
    }
        cin  >> ans2;


I'm not having any bugs, but this is more to see if there is something further I need to do in order to get my desired result.

My desired result is as follows:


Player 1:
     Who was the first President of the United States?
     1. Abe Lincoln
     2. LBJ
     3. George Washington
     4. Teddy Roosevelt

Player 2:
     Who was the first President of the United States?
     1. Teddy Roosevelt
     2. Abe Lincoln
     3. George Washington
     4. LBJ


Ok, so the aforementioned code works -too- perfectly. It generates my questions as expected and prints out the possible answers randomly. The problem is that sometimes the output is as follows:


Player 1:
     Who was the first President of the United States?
     1. Abe Lincoln
     2. Abe Lincoln
     3. George Washington
     4. Teddy Roosevelt

Player 2:
     Who was the first President of the United States?
     1. Teddy Roosevelt
     2. Teddy Roosevelt
     3. George Washington
     4. Teddy Roosevelt


Is there anything else I could be doing or maybe some other suggestion as to how to handle this situation?
You could push your randIndex into a vector and check against those values in your vector until randIndex returns a value that wasn't previously used. I'm assuming this would keep

q1possible[randIndex] from repeating with the same index.

As you probably know. Rand isn't really random and each time you run it that way you'll get the same results. For example,

1
2
3
4
5
6
            for(int i = 0; i < 8; i++)
    {

        int randIndex = rand() %4; // generates a random number between 0 and 3
        cout << randIndex << endl;
    }


that returns
1
3
2
0
1
0
2
2
Every time I run it on my machine. Even if I changed it to the original 4 iterations and then run that for loop again it yields repeating values. Maybe there's a simpler way but I don't think vectors would be bad.
Last edited on
A few remarks:
1. You use the same index for the outer and inner loop. That is VERY BAD. I am surprised that your compiler did not complain about that. Or maybe it's just a typo
2. Suppose you have 4 possible answers. Store numbers 0 to 3 in a vector. The first answer can be any of those. You select it with rand()%4. If you do not want to see that answer again, you must erase the corresponding index from the vector. You are left with only 3 answers. So to select the next one, you should use rand()%3. And for the next one is rand()%2. For the last one you do not need to use rand at all, since you have only one answer left
1. You use the same index for the outer and inner loop. That is VERY BAD. I am surprised that your compiler did not complain about that. Or maybe it's just a typo

why would it? it is perfectly valid c++ to have two variables with the same name in c++
If he was to use vectors, why not just use random_shuffle() from algorithm instead of messing around with loops and modulus?

http://www.cplusplus.com/reference/algorithm/random_shuffle/
I finally got it figured out! I would very much like to thank all who offered insight/help/tips on my question. I eventually ended up using fg109's suggestion and used random_shuffle at the top of my for loop. It was more efficient for me (my opinion). I know I didn't give much code to look at or work with, but I really wanted to try and figure out as much as possible on my own.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
char ans2;

for (int i = 0; i < 4; ++i)
     {
          keep.push_back(i);
     }

for(int i = 0; i < 10; i++)
{
     random_shuffle (keep.begin(), keep.end());
     cout << p2Q[i].getQuestion() << endl;

     for(int i = 0; i < 4; i++)
          {
               cout << q1Possible[keep[i]] << endl;
          }
}
     cin >> ans2;


Now, all I have left to do is figure out how to keep track of the correct answer with all the randomness going on. I'll come back for help if I run into a wall. Thanks again!
Last edited on
Topic archived. No new replies allowed.