C# List Random не понятна логика

    private List<List<string>> randomIndexes()
    {
        List<List<string>> allIndexes = TakeDistrictIndexes();
        List<List<string>> randIndex = createDistrictsIndexes();
        int m1;
        int m2;
        for (int i = 0; i < allIndexes.Count; i+=2)
        {
            List<int> Oldindexes1 = new List<int> { };
            List<int> Oldindexes2 = new List<int> { };
            int city = allIndexes[i].Count - 1;
            int village = allIndexes[i + 1].Count - 1;

            for (int j = 0; j < Convert.ToInt32(dataGridView2[1,i/2].Value); j++)
            {
                m1 = TakeRandomIndex(Oldindexes1, city);
                
                randIndex[i].Add(allIndexes[i][m1 + 1]);
                //MessageBox.Show(randIndex[i][j+1]);
                Oldindexes1.Add(m1);                }
            for (int j = 0; j < Convert.ToInt32(dataGridView2[2, i/2].Value); j++)
            {
                m2 = TakeRandomIndex(Oldindexes2, village);
                randIndex[i+1].Add(allIndexes[i+1][m2 + 1]);
                //MessageBox.Show(randIndex[i + 1][j + 1]);
                Oldindexes2.Add(m2);
            }
            Print.print(randIndex);
        }
        return randIndex;
    }
        private int TakeRandomIndex(List<int> oldIndexes,int s2)
    {
        Random rnd = new Random();
        int rndInd = -1;
        //bool isIn = true;
        rndInd = rnd.Next(0, s2);
        /*while (isIn = true)
        {
            
            if(oldIndexes.IndexOf(rndInd) < 0)
            {
                isIn = false;   
            }
        }*/
        return rndInd;
    }

В таком варианте выводит одинаковые индексы на выходе, что бред по сути.

Без MessageBox.Show(randIndex[i + 1][j + 1]); и MessageBox.Show(randIndex[i][j+1]);

img

Но когда я использую MessageBox.Show(randIndex[i + 1][j + 1]); и MessageBox.Show(randIndex[i][j+1]); получаются разные значения как и должно быть. Я вообще не вижу логики, помогите кто-нибудь.

введите сюда описание изображения


Ответы (0 шт):