How can I random quiz questions in C++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

How can I random quiz questions in C++?

Have created a quiz game. I need the questions to randomly display when i open the program.

16th Apr 2019, 7:29 AM
Coder learner
Coder learner - avatar
10 Answers
+ 9
Lucky Bill Your question gives no sense. Please explain.
16th Apr 2019, 7:39 AM
Sarthak Pokhrel
Sarthak Pokhrel - avatar
+ 6
Lets say you have a string array string arr[100]; which contains all the questions now we generate a number which is less than maximum size srand(time(NULL)); int random = (rand()%100); now get the question with the random index number cout << arr[random]; NOTE:- <cstdlib> is needed for rand() and srand() <ctime> is needed for time()
16th Apr 2019, 2:35 PM
Shahil Ahmed
Shahil Ahmed - avatar
+ 5
What exactly do you mean? Randomize the order of certain questions or picking a random question (from an array of questions e.g.)
16th Apr 2019, 7:38 AM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 4
If you want to choose a question randomly from let's say a vector of questions: You'd use the function rand to generate a random number from 0 to size of your vector minus 1 and use that number for accessing the question. cout << your_vector.at( your_random_number) https://en.cppreference.com/w/cpp/numeric/random/rand
16th Apr 2019, 8:19 AM
HonFu
HonFu - avatar
+ 4
Lucky Bill You can also use cout<<arr[(rand()%100)] ; directly I showed that integer for explanation
17th Apr 2019, 8:47 AM
Shahil Ahmed
Shahil Ahmed - avatar
+ 4
You can add your quiz game to SL Code Playground and save it. Other users may find it useful.
18th Apr 2019, 7:30 AM
Sonic
Sonic - avatar
+ 3
Flaming Arrow thanks.. Let me try that out..
17th Apr 2019, 8:43 AM
Coder learner
Coder learner - avatar
+ 2
Aaron Eberhardt i want to make my quiz program questions to display randomly when i open the program......
17th Apr 2019, 5:03 PM
Coder learner
Coder learner - avatar
5th May 2019, 3:36 PM
Coder learner
Coder learner - avatar
0
yes
8th Jul 2023, 1:31 AM
maze khan
maze khan - avatar