简体   繁体   English

如何检查是否存在相同的 Random 对象

[英]How to check there is the same Random object

I'm doing simple Xamarin.Forms puzzle game and i need to have 9 puzzles with diffrent random values.我正在做简单的 Xamarin.Forms 益智游戏,我需要有 9 个具有不同随机值的谜题。 I tried to check it with some loops, but it's still not working.我试图用一些循环来检查它,但它仍然无法正常工作。

Random r = new Random();

            Label[] puzzles = { puz1, puz2, puz3, puz4, puz5, puz6, puz7, puz8, puz9 };
            string[] used = new string[9];
            for (int i = 0; i < puzzles.Length; i++)
            {
                if (i > 0)
                {
                    for (int x = 1; x < used.Length; x++)
                    {
                        do
                        {
                            puzzles[i].Text = Puzzles.puz[r.Next(0, 8)];
                            used[x] = puzzles[i].Text;
                        }
                        while (used[x - 1] == used[x]);
                    }
                }
                else
                {
                    puzzles[i].Text = Puzzles.puz[r.Next(0, 8)];
                    used[0] = puzzles[i].Text;
                }
            }

And Puzzles.cs class和 Puzzles.cs 类

class Puzzles
    {
        public static string[] puz = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };


    }

How can I check that new generated puzzle hasn't the same value that puzzles generated previously?如何检查新生成的拼图与之前生成的拼图的值不同?

This is because you only check the preceding value for duplicates, making it still possible for used[x -2] == used[x] to be true.这是因为您只检查前面的值是否有重复,这使得used[x -2] == used[x]仍然可能为真。

To achieve your goal, I would suggest implementing a shuffle function, like the one you can find here .为了实现您的目标,我建议您实现一个随机播放功能,就像您可以在这里找到的那​​样 It can give something like this它可以给出这样的东西

// Implemented somewhere in your code
private List<E> ShuffleList<E>(List<E> inputList)
{
     List<E> randomList = new List<E>();

     Random r = new Random();
     int randomIndex = 0;
     while (inputList.Count > 0)
     {
          randomIndex = r.Next(0, inputList.Count); //Choose a random object in the list
          randomList.Add(inputList[randomIndex]); //add it to the new, random list
          inputList.RemoveAt(randomIndex); //remove to avoid duplicates
     }

     return randomList; //return the new random list
}

// Then for each element of your puzzles array, you could do
puzzles[i].Text = SuffleList(Puzzles.puz);

Thanks all for help, I didn't know Shuffle mechanism.感谢大家的帮助,我不知道 Shuffle 机制。 Finally my 'working' code presents as follows最后我的“工作”代码如下

        static Random rnd = new Random();

        static void Shuffle<T>(T[] array)
        {
            int n = array.Length;
            for (int i = 0; i < n; i++)
            {
                int r = i + rnd.Next(n - i);
                T t = array[r];
                array[r] = array[i];
                array[i] = t;
            }
        }

        public Game()
        {
            InitializeComponent();


            Label[] puzzles = { puz1, puz2, puz3, puz4, puz5, puz6, puz7, puz8, puz9 };

            string[] puz = { "1", "2", "3", "4", "5", "6", "7", "8" };

            Shuffle(puz);
            for (int i = 0; i < puzzles.Length - 1; i++)
            {
                puzzles[i].Text = puz[i];
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM