c# - Generation of new random items from a List -


i have code generates new random item ist without repetition.

   class text_generator      {        public int getwordindex(list<string> source, random value)         {           return value.next(0, source.count - 1);         }           public bool checklistlength(list<string> source)         {           return source.count == 0;         }        public string gettext(list<string> source, list<string>                backup_source, random value)         {           if (checklistlength(source))           {             source.addrange(backup_source);           }           ;           int index = getwordindex(source, value);           string result = source[index];           source.removeat(index);           return result;       }   } 

then open main list , empty list.

text_generator textix = new text_generator(); list<string> hi = new list<string> { "hi", "howdy", "hey" //etc }; list<string> work_hi = new list<string>(); 

and... generate. different until elements used.

random rand = new random(); console.writeline(textix.gettext(work_hi, hi, rand)); 

my question is: while code works fine, seems bit long. possible make same 1 method? possible not open 1 more list? how can it?

have considered sorting list random order?

random rand = new random(); list<string> hi = new list<string> { "hi", "howdy", "hey" }; list<string> work_hi = hi.orderby(x => rand.next()).tolist(); 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -