1

I would like to know how to achieve this task in C#. For example;

I got 10 questions from which 3 is to be displayed to user for them to type the answer. How can i make the program generate 3 questions that are non-repeating(unique) assuming the 10 questions that we are starting with is unique.

I am using the logic in a asp.net application, and the same set of questions are allowed to be displayed the next time page is refreshed, so that's no problem for me.

Deeptechtons
  • 10,945
  • 27
  • 96
  • 178

3 Answers3

3

Use a List for your Question instances, and select one at random (by index). Then remove it from the List and repeat. Something like so;

    static void Main(string[] args)
    {
        List<string> questions = new List<string>();
        for (int i = 0; i < 10; i++)
            questions.Add("Question " + i);

        Random r = new Random();
        for (int i = 0; i < 3; i++)
        {
            int nextQuestion = r.Next(0, questions.Count);
            Console.WriteLine(questions[nextQuestion]);
            questions.RemoveAt(nextQuestion);
        }
    }
RJ Lohan
  • 6,497
  • 3
  • 34
  • 54
  • Yup you were right. I thought for some reason using the same Random object would produce a unique number each time it was used but it does not. You have to track them your self in some kind of collection. – Colin Pear Oct 08 '12 at 04:29
1

One of approaches is to shuffle elements randomly and then pick first three of them. For how to shuffle in C# - Randomize a List<T>.
This approach is better than remove questions from list for big collection because, in the worst case(when randomizing is determined or just happenned badly) it can grow up to O(n^2) due to O(n) complexity of removing.

Community
  • 1
  • 1
htzfun
  • 1,231
  • 10
  • 41
0
class Questions
{
    const int NUMBER_OF_QUESTIONS = 10;
    readonly List<string> questionsList;
    private bool[] avoidQuestions; // this is the "do-not-ask-question" list
    public Questions()
    {
        avoidQuestions = new bool[NUMBER_OF_QUESTIONS];

        questionsList = new List<string>
                            {
                                "question1",
                                "question2",
                                "question3",
                                "question4",
                                "question5",
                                "question6",
                                "question7",
                                "question8",
                                "question9"
                            };            
    }

    public string GetQuestion()
    {
        Random rnd = new Random();
        int randomVal;

        // get a new question if this question is on the "do not ask question" list
        do
        {
            randomVal =  rnd.Next(0, NUMBER_OF_QUESTIONS -1);
        } while (avoidQuestions[randomVal]);

        // do not allow this question to be selected again
        avoidQuestions[randomVal] = true;

        // do not allow question before this one to be selected
        if (randomVal != 0)
        {
            avoidQuestions[randomVal - 1] = true;
        }

        // do not allow question after this one to be selected
        if (randomVal != NUMBER_OF_QUESTIONS - 1)
        {
            avoidQuestions[randomVal + 1] = true;
        }

        return questionsList[randomVal];
    }
}

Just create Questions object and call questions.GetQuestions() three times

Srini
  • 446
  • 5
  • 11