伪随机数生成器 C#

Pseudo Random Number Generator C#

Random randomSeed = new Random();
int seed = randomSeed.Next(255);

String display = "";
int min = 1;
int max = 10;
int number;

Random rand = new Random(seed);

for (int i = 0; i < max; i++)
    {
        number = rand.Next(min, max);
        display += "\t" + number;
    }
    rtOutput.Text = display;

我正在尝试使用 Visual C# 获取 PRNG。 我遇到的问题是数字不断重复。 这是 1 随机的结果: 2 6 3 7 9 7 9 3 3 7

从结果来看,数字3,7,9重复了。 知道我的错误代码在哪里吗? 有什么办法让它不重复相同的数字吗?

`我终于得到了自己的 PRNG,经过大量尝试,这是我的代码:

        // Manually input the Seed, or you can make it random like my code above.
        int seed = Convert.ToInt32(tbSeed.Text);

        String display = "";
        int min = 1;
        // Max value is manually input, for how many number will be generated.
        // i need to plus by 1 for the max value because i state the min value is 1.
        int max = Convert.ToInt32(tbMax.Text) + 1;

        Random rand = new Random(seed);


        int number;
        // this dictionary is for saving the number generated by random, if exist, 
        //do random again.
        Dictionary<int, int> num = new Dictionary<int,int>();

        for (int i = 1; i < max; i++)
        {

            number = rand.Next(min, max);
            if (num.ContainsKey(number))
            {
                while (true)
                {
                    number = rand.Next(min, max);
                    if (num.ContainsKey(number))
                    { // if exist do nothing and then random again while true  }
                    else
                    {
                        num.Add(number, 1);
                        break;
                    }

                }
            }
            else
            {
                num.Add(number, 1);
            }

            display += "\t" + number;

        }
        // display the random number.
        rtOutput.Text = display;

`

您想要的是随机 "ordered" 列表,而不是随机 "generated" 列表。这通常称为 "Shuffling".

这里有一些示例代码(无论如何都不是好的代码,但我把它作为示例尽可能接近你的代码)来实现你想要的:

static void Shuffle(int[] list)
{
  var rnd = new Random();
  int n = list.Count();
  while (n > 1)
  {
    n--;
    int k = rnd.Next(n + 1);
    int value = list[k];
    list[k] = list[n];
    list[n] = value;
  }
}

static void Main(string[] args)
{
  int min = 1;
  int max = 10;
  int [] numbers = new int[max-min];

  for (int i = min; i < max; i++)
    numbers[i-min] = i;

  Shuffle(numbers);

  string display = "";
  for (int i = min; i < max; i++)
    display += " " + numbers[i-min];
  Console.Write(display);
}

结果应该是这样的:

4 9 2 1 3 7 5 8 6

应该适用于任何 minmax 值(生成与中间值一样多的数字)

// all the numbers we want to use (you could also generate this programmatically)
List<int> oneToTen = new List<int> {1,2,3,4,5,6,7,8,9,10}; 

String display = "";
int number;
Random rand = new Random();

for (int i = 0; i < 10; i++) {
    int randomIndex = rand.Next(0, oneToTen.Count); // choose one at random
    number = oneToTen[randomIndex];
    oneToTen.Remove(number); // remove it so we don't choose it agian
    display += "\t" + number;
}