mardi 27 octobre 2015

Only add random number to list in loop when doesn't already exist

New to using lists. For automated testing purposes I am generating a list of distinct values. The following is a code block of concern:

Random rnd = new Random();
List<int> lVars = new List<int>();

        while (VarsCount < randVarsCount)
        {
            if(VarsCount > 0)
            {
                while(lVars.Distinct().Count() != lVars.Count()) 
                {
                    lRowVars.Insert(VarsCount, rnd.Next(1, 11)); //problem code 
                }
            }
            lVars.Add(rnd.Next(1, 11));
            MessageBox.Show(lRowVars[aRowVarsCounter].ToString());
            aRowVarsCounter++;
        }

Basically, how do I check to see if the int being added matches all of the list (as my code doesn't work)....I've tried some other code but ends up being ALOT of extra code and loops; usually when I feel I'm doing something superfluous I find there is an easier way.




Aucun commentaire:

Enregistrer un commentaire