Selecting a random question from a pool of questions until all questions are selected.
Show older comments
Hello. I am trying to create a random question generator for a project of mine but I do not know how to program matlab to pull out a question from a pool of questions. I was thinking that one would give values to each question and then have matlab randomly select a value that matches one of the questions but the only thing I know to do is to make matlab randomly output a number. Is there a way to make matlab output a random variable? Thank you!!!!!
1 Comment
Guillaume
on 12 Mar 2015
Is there a way to make matlab output a random variable?
Hopefully, each of your question is not held in its own variable. They are all in a cell array or similar container. In which case, it's a simple matter of using randperm as per James' answer and indexing the cell array.
Answers (1)
James Tursa
on 12 Mar 2015
If you have n questions, then
q = randperm(n);
Gives you a random ordering of the numbers 1 through n. You can use that to cycle through the questions randomly and hit every question without repeats.
5 Comments
Lovablepanda609
on 12 Mar 2015
James Tursa
on 12 Mar 2015
I would follow Guillaume's advice to use a cell array of strings.
So you would have:
questions = {'What is your name'
'What is your quest'
'What is the airspeed velocity of an unladen swallow'}
qorder = randperm(numel(questions));
qreordered = questions(qorder)
You could even store the answers together with your questions in the same cell array
QandA = {'What is your name', 'Arthur, King of the Britons';
'What is your quest', 'To seek the Holy Grail';
'What is the airspeed velocity of an unladen swallow', 'African or European?'}
qorder = randperm(numel(questions));
QandAreordered = QandA(qorder, :)
for qidx = 1:size(QandAreordered, 1)
answer = input(QandAreordered{qidx, 1}, 's');
if strcmp([answer ' '], QandAreordered{qidx, 2})
fprintf('Correct\n');
else
fprintf('You''re thrown off the bridge\n');
end
end
James Tursa
on 12 Mar 2015
What do you mean ... an African Swallow or a European Swallow?
Laiza Perez
on 2 Dec 2020
I know the questions are randomly selected but how do you get the answers to also reorder to match the questions that are reordered?
Categories
Find more on PHY Components in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!