Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Experts,
C#
private int GetRandomNumber(int[] levelIds)
       {
           bool exists = false;
           int randomIndex = 0;
           do
           {
               Random rand = new Random();
               randomIndex = rand.Next(levelIds.Length);
               if (completedQuestionIds.Count > 0)
                   exists = completedQuestionIds.Contains(levelIds[randomIndex]);
           }
           while (exists == true);

           completedQuestionIds.Add(levelIds[randomIndex]);

           Session["CompletedQuestionIds"] = completedQuestionIds;

           return levelIds[randomIndex];
       }

Thanks in advance for your valuable reply
Posted
Updated 20-Nov-11 22:42pm
v2

1 solution

Hi Subbu,

I have created sample code for you, please modify your code according this.

This code is very SIMPLE and FAST to pickup random question from list of questions.
C#
ArrayList availableQuestions = new ArrayList();
ArrayList completedQuestions = new ArrayList();
public Form1()
{
    InitializeComponent();

    for (int i = 1; i <= 10; i++)
    {
        availableQuestions.Add(i);
    }
}
private void button1_Click(object sender, EventArgs e)
{
    GetRandomNumber();
}
private void GetRandomNumber()
{
    if (availableQuestions.Count >= 1)
    {
        Random rand = new Random();
        int QuestionID = Convert.ToInt16(availableQuestions[rand.Next(0, availableQuestions.Count - 1)]);
        completedQuestions.Add(QuestionID);
        availableQuestions.Remove(QuestionID);
    }
}

Please do let me know, if you have any doubt for the same.

Thanks,
Imdadhusen
 
Share this answer
 
Comments
2011999 21-Nov-11 6:48am    
thank u for ur replay
Sunasara Imdadhusen 21-Nov-11 7:23am    
Have you implemented in your code? is this correct solution?
Please do let me know still if you have any doubt!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900