0

I added some string into list from database , i random the string order and display it but i don't want the random string to appear again , so i did a remove(string) however it won't work .

So i declared a list string like this :

 List<string> questionNo = new List<string>();

This is the code i put in on PageLoad( outside of !Page.IsPostBack) :

  protected void RandomMCQ1()
{
    Random r = new Random();
    int index = r.Next(questionNo.Count());
    randomString = questionNo[index];   // Random a string from list ( I declare randomString as a string in global)
    questionNo.Remove(randomString);    // Then remove it
}

So i want to test it by displaying randomString after a button clicking like this :

  protected void btnNext_Click(object sender, EventArgs e)
    {
        Response.Write(questionNo.Count); // Display list<string> count
        Response.Write(randomString); // Display the random string .
    }

I have 2 values in the list : 10 and 11

so the count of the list is 2 The count is correct when i click the button ( the count suppose to be 2 , after i click the button , it reduces to 1 ) but the random string can display 10 or 11 , shouldn't it be when i click the button the first time , it display either 10 or 11 , if it displays 10 , the second i click the button it should display 11 then the 3rd time i press it should not display anything .

---EDIT-----

This is how i get my list ( this code is in PageLoad outside of !Page.IsPostBack)

 protected void PopulateMCQ()
{
    string query = "...";

    conn.Open();

    SqlCommand cmd = new SqlCommand(query, conn);
    SqlDataReader dr;
    dr = cmd.ExecuteReader();
    while (dr.Read())
    {
        Label questionID = new Label();
        questionID.Text = dr["englishID"].ToString();
        questionNo.Add(questionID.Text); // I add the values to the list<string> here

    }


    conn.Close();

}
user2376998
  • 1,061
  • 7
  • 32
  • 65
  • Something is missing from your example because it would appear you never call `RandomMCQ1()`? – lc. Feb 07 '14 at 01:52
  • I did post that i put RandomMCQ1() in the page load – user2376998 Feb 07 '14 at 01:53
  • I can't put RandomMCQ1() in the button , it will re populate the list everytime i hit the button – user2376998 Feb 07 '14 at 01:53
  • If you want to display the entire list in a random order, you may want to permute the list. Instead of picking a random element of the list and printing it, first re-arrange the list randomly and then print it. [This other answer](http://stackoverflow.com/questions/3152244/how-to-permute-a-list-of-objects-in-c) might help. – Brad Spencer Feb 07 '14 at 01:53
  • Whats permute? i don't understand , is there no other way to do it rather than permute? – user2376998 Feb 07 '14 at 01:55
  • Permute is a fancy way of saying shuffle the list. Then you can just take the items off from start to end (as shuffling once is as random as shuffling each time something is removed). – SeeMoreGain Feb 07 '14 at 02:17

2 Answers2

0

Permute (shuffle) the list, then just return the elements one by one from start to end. This will be the most efficient way to do what you want.

For code for shuffling the list see this question: Randomize a List<T>

Community
  • 1
  • 1
SeeMoreGain
  • 1,263
  • 16
  • 36
0

Most likely issue is with how/when you are populating questionNo. Since it is just regular object and you did not show how you store it between request there is a good chance you always read it from DB in the beginning of the request.

So on first GET request you get some random value from the list, but on subsequent POST requests for click you likely reloading questionNo from DB and thus it is always showing your default values.

If it is indeed the problem to fix you need to persist your questionNo and random choice. Easiest is to save both into Session object on first request and read from Session on postback.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • Please guide me along using Session , u meant store the values in the session and then random the session values ? – user2376998 Feb 07 '14 at 02:48
  • @user2376998 - yes. As I said - read from DB on first request and put in session state (`Session["foo"]= questionNo`). On postback read from `Session` - `questionNo = (List)Session["foo"]`, similar for the choice. – Alexei Levenkov Feb 07 '14 at 04:07