Unique random number generation in c# -


i using listing #1 develop multiple-choice q&a application.

  1. i retrieve, say, 1000 entries database.
  2. i select 6 random questions them , display them in form.
  3. i select 1 random question out of 6 ask.

enter image description here

as can see, listing #1 has 1 serious issue. may generate duplicate numbers in each step , thereby asks same question more once (to wipe off repeated).

to solve this, have written listing #2.

but, listing #2 has serious issues:

  1. the call new uniquerandomnumber().next(0, 5); never returns 5.
  2. if _list.count near 5-7, stuck in while loop ever.
  3. i observed that, randomquestion.next() returns 0 of times.

listing #1

    private void startlearning()     {         if (_list != null)         {             _list.clear();             _list = null;         }          _list = _worddatabase.getbycorrectnessbelow(correctness_repetition);          if (_list != null)         {             if (_list.count >= multiple_choice_count)             {                 if (_selectedwords != null)                 {                     _selectedwords.clear();                     _selectedwords = null;                 }                  _selectedwords = new list<word>(multiple_choice_count);                  int x = 25, y = 50;                  groupbox1.controls.clear();                 groupbox2.controls.clear();                 //obtain 6 random words _list                 //create 6 buttons , 6 labels                 random randomword = new random();                 (int = 0; < multiple_choice_count; i++)                 {                     //obtain random index                     int listindex = randomword.next(0, _list.count - 1);                     word item = _list[listindex];                     _selectedwords.add(item);                      //create button                     button b = new button();                     b.tag = i;                     b.text = item.name.tolower()+" ("+ item.correctnesscount.tostring()+")" ;                     b.size = new system.drawing.size(225, 45);                     b.font = new system.drawing.font("calibri", 18);                     b.location = new point(x, y);                     b.autosize = false;                     b.textalign = contentalignment.middlecenter;                     b.mouseclick += wordbutton_mouseclick;                     groupbox1.controls.add(b);                      //create label                       label l = new label();                     l.tag = i;                     l.autosize = false;                     l.text = item.meaning.tolower();                     l.size = new system.drawing.size(475, 45);                     l.font = new system.drawing.font("verdana", 16);                     l.textalign = contentalignment.middleleft;                                             l.location = new point(x, y);                     l.borderstyle = borderstyle.fixed3d;                     l.backcolor = systemcolors.info;                     groupbox2.controls.add(l);                      y = y + 65;                 }                  resetanswerbuttoncolors();                 enablebuttons(true);                 visiblelabels(false);                  //select random word 6                 //to ask question.                 random randomquestion = new random();                 _questionindex = randomquestion.next(0, 5);                 word w = _selectedwords[_questionindex];                  answertextbox.tag = w;                 answertextbox.text = w.meaning.tolower();             }         }     } 

listing #2

public class uniquerandomnumber {     private random random = new random();     private list<int> randomlist = new list<int>();      public int next(int min, int max)     {         int newint = 0;          while(true)         {             newint = random.next(min, max);                              if(!randomlist.contains(newint))             {                 randomlist.add(newint);                 break;             }         }          return newint;     } } 


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -