Randomize

Pages: 12
I make a game that will ask you question. But the thing is, how can I randomize the questions everytime I open the program? Also, can I assign the questions to string? Thanks.
Put the questions in an array or vector. Then choose a random number within the range of the size of the container and use that to select the question.

rand() returns you a random number between 0 and some large number.
(rand() % 6) returns you a random number between 0 and 5.

srand(some number) "seeds" the generator, you need to call it once in the program before you call rand().
how can I randomize the questions everytime I open the program?
Depending on how is it stored.
if you have a vector of Question objects:
1
2
3
4
std::vector<Question> questions = /*...*/;
std::mt19937 random_engine(std::time(nullptr)); //Better to use random_device, but it does not work for MinGW
std::shuffle(questions.begin(), questions.end(), random_engine);
//now questions in random order 


can I assign the questions to string?
Yes
Sir @kbw, I don't know array either vector :( How can it be possible? I made three questions sir.
Sir @MiiNiPaa will try sir. But can you explain it more?
If you have 3 questions. You can use the random function srand() and rand, to make it pick a number between 1 and 3, and then put that random generator number in an integer variable. Then you can do if statements, saying. If the random number is equal to 1, ask this question, if its equal to 2, ask this question, if its equal to 3, ask that question.
Something like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
srand(time(NULL));
	int x = rand() % 3 + 1;
	cout << x;

	if (x == 1)
	{
		...
	}
	else if (x == 2)
	{
		...
	}
	else if (x == 3)
	{
		...
	}


oh and include #include <ctime> for the "time". Everytime you run the program x will will be assigned a number between 1-3. depending on what it is, it will ask whatever question you put in the if statements.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <algorithm>
#include <ctime>
#include <iostream>
#include <random>
#include <string>
#include <vector>

struct Question
{
    std::string question;
    std::string answer;
};

bool ask_question(const Question& q)
{
    std::cout << q.question << '\n';
    std::string answer;
    std::cin >> answer;
    if(answer == q.answer) {
        std::cout << "Correct!\n";
        return true;
    } else {
        std::cout << "Wrong! Correct answer is: " << q.answer << '\n';
        return false;
    }
}

int main()
{
    //http://www.mochima.com/tutorials/vectors.html
    std::vector<Question> questions {
        {"Is year 1900 leap?", "no"}, {"How many rings in Olympics symbol?", "five"},
        {"What will water transform to when temperature is below 0°C?", "ice"}
    };
    //http://en.cppreference.com/w/cpp/numeric/random
    std::mt19937 random_engine(std::time(nullptr));
    random_engine.discard(8);

    //http://en.cppreference.com/w/cpp/algorithm/random_shuffle
    std::shuffle(questions.begin(), questions.end(), random_engine);

    int score = 0;
    for(const auto& q: questions)
        if(ask_question(q))
            ++score;
    std::cout << "Your score is: " << score << '\n';
}

Last edited on
Let's assume your questions are strings, you can initialise a vector of them with:
1
2
3
4
5
6
std::vector<std::string> questions =
{
    "What is your name?",
    "Where were you born?",
    "What is the date?"
};


That will generate a vector named questions. You can ask the vector things like it's size, with questions.size(). And you can use that size expression in the rand() example I posted above.
@MiiNiPaa. He hasnt even learned about arrays, I do not think that he wants to use vector what so ever. I think that progarm is way too complicated for what he is asking for. Im fairly certain he is looking for the one I provided.
Woah! Thanks so much for the help guys. @TarikNeaj is right, sadly, I don't know arrays and vectors :(
I suggest to read vector tutorial avaliable in link provided in comment before vector declaration.

I have a strong opinion (shared by C++ creator) that vector should be learned before arrays: arrays are more tricky, less expressive and are generally a "sharp razor" of C/C++
I 100% agree with you @MiiNiPaa. But, if you are studying c++ in a university, more often or not they will always teach you about arrays and not vectors, (to begin with atleast). Sometimes you get assignments where you must use arrays and can not use vectors.

@Ryanjoshiii if that is the case, learning about arrays is very important, and for simple things like this one its really not that hard (will link to a good tutorial at the end here). But if your univerity, allows you to use vectors, or you're learning by yourself, then use them.

Bucky has very nice tutorials on the basics of c++. You can look up the ones for arrays -

https://www.youtube.com/playlist?list=PLAE85DE8440AA6B83

If you want to learn about vectors you can just google it or watch videos on youtube, there's some good ones out there.
Thank you so much guys! You are all awesome :) Somehow, I manage to randomize questions. I used sir @TarikNeaj codes :) I promise to study arrays and vector :)

But theirs another problem, if I randomized questions, I randonmized the choices, how about to check the answers correctly?
@ryanjoshiii what do you mean by

>how about to check the answers correctly?
Its like who wants to be a millionaire sir. How can I validate their answer? If I randomized the questions, how can I check the answers? Sample.

On the first run, the question no. 1 is Who painted Mona Lisa? a. DaVinci b. DeCaprio c. Picasso d. Angelo

The correct answer is a. But how can I check if the questions are randomized? Do I need to make conditions for each questions?
But how can I check if the questions are randomized?


The questions are randomized because you wrote code that randomizes them. Are you sure you didint mean something else? Also, please post all the code you have now.
But how can I check if the questions are randomized?
Group question, possible answer and correct answer in structure.
http://www.cplusplus.com/doc/tutorial/structures/
This way you will hold question and answer together allowing you to get answer from question itself.

All other alternative approaches are as effective and sensible as rectal tonsillectomy.
Again @MiiNiPaa. He doesnt even know what an array is, I dont think structures are gonna be helpful or even allowed to be used (if he's studying in university that is), but I could be completely wrong.

OP, do you study in university or self learning, what are you allowed to use?
I think you're just communicating what you want in a bit of a confusing way though.
This is the codes guysm but it doesn't run. The error is the string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <conio.h>
#include <ctime>
#include <string>
using namespace std;

int main()
{
	string quest1;
	char a,b,c,d;

	srand(time(NULL));
	int x = rand() % 3 + 1;


	if (x == 1)
	{
		quest1 = "\nIf the dog died last night. Did the dog died?";
		cout<<"\na. Yes					b. The dog is immortal";
		cout<<"\nc. No					d. Just soccery";
	}
	else if (x == 2)
	{
		quest1 = "\nWho painted Mona Lisa?";
	}
	else if (x == 3)
	{
		quest1 = "\nWho is the first man in the moon?";
	}

	cout<<"\n\n\n\n\nQuestion no.1"<<quest1;
	cout<<"Enter your choice: ";
	cin>>a;

	switch(quest1)
	{
	case 1:
		switch (a)
		{
		case 'a':
			{
				cout<<"Correct!";
			}
		case 'b':
			{
				cout<<"Wrong!";
			}
		case 'c':
			{
				cout<<"Wrong!";
			}
		case 'd':
			{
				cout<<"Wrong!";
			}
		}
	}
	getch();
	return 0;
}
Pages: 12