Computer Science Canada

I'm making a quiz, in which I have to ask 10 questions randomly out of 20, how do I make sure no question is repeated?

Author:  mythili [ Sat Jan 10, 2015 11:29 pm ]
Post subject:  I'm making a quiz, in which I have to ask 10 questions randomly out of 20, how do I make sure no question is repeated?

What is it you are trying to achieve?
I'm making a personality quiz in which I have twenty questions but when the program is run only ten are asked randomly


What is the problem you are having?
A question appears more than once


Describe what you have tried to solve this problem
<Answer Here>


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>

Turing:


I attached a file



Please specify what version of Turing you are using
The latest version

Author:  Insectoid [ Sun Jan 11, 2015 12:41 am ]
Post subject:  RE:I\'m making a quiz, in which I have to ask 10 questions randomly out of 20, how do I make sure no question is repeate

You could try shuffling them. There are plenty of shuffling algorithms and tutorials that you can look up.

Author:  masterofcode [ Sun Jan 11, 2015 1:01 pm ]
Post subject:  Re: I'm making a quiz, in which I have to ask 10 questions randomly out of 20, how do I make sure no question is repeate

Hi Mythili,

What you can do is create an array from 1 to 20 and based on the randomly picked question you change the value of the corresponding array value. We'll say its 1 or 2. When it picks the random question, have the program first check the corresponding value in the array. If it is 1 then you can ask the question, if it is 2 it registers as already asked and won't ask the question again.

For exapmle:

var questionArray : array 1 .. 20 of int
var questionNum : int := Rand.Int (1, 20)
if questionArray (questionNum) not= 2 then
<Ask the corresponding question>
questionArray (questionNum) = 2
end if

Incorporate something along those lines into your code.

This is Niam by the way so ask me in class tomorrow if you still can't get it working. Laughing

Author:  Insectoid [ Sun Jan 11, 2015 10:06 pm ]
Post subject:  RE:I\'m making a quiz, in which I have to ask 10 questions randomly out of 20, how do I make sure no question is repeate

That's an absolutely terrible way to do it. Your code will take an arbitrary time to execute, which means that it could take 5 seconds or 5 hours, or run until the computer fails. You'd be much better off using a real shuffling algorithm.

Author:  mythili [ Mon Jan 12, 2015 7:49 pm ]
Post subject:  Re: I'm making a quiz, in which I have to ask 10 questions randomly out of 20, how do I make sure no question is repeate

I'm sorry, I don't know what shuffling is, can you please explain? Smile

Author:  Insectoid [ Mon Jan 12, 2015 8:37 pm ]
Post subject:  RE:I\'m making a quiz, in which I have to ask 10 questions randomly out of 20, how do I make sure no question is repeate

Like shuffling a deck of cards. There are plenty of very simple algorithms you can use that will do this. You can probably even make one up yourself. The important piece is figuring out how to swap two 'cards' (in this case, questions).


: