简体   繁体   English

伪随机数生成器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;

I'm trying to get a PRNG with visual c#. 我正在尝试使用可视C#获得PRNG。 the problem i had is the number keep repeating. 我遇到的问题是人数不断重复。 this is the result of 1 random : 2 6 3 7 9 7 9 3 3 7 这是1随机的结果:2 6 3 7 9 7 9 3 3 7

from the result, the number 3,7,9 are repeating. 从结果来看,数字3,7,9在重复。 any idea where is my wrong code? 知道我的错误代码在哪里吗? any solution to make it not repeating the same number? 任何解决方案,以使其不重复相同的数字?

` finally i got my own PRNG, after lot of trying, here is my code : ` 经过大量尝试,终于有了自己的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;

` `

What you want is a randomly "ordered" list, not a randomly "generated" list. 您想要的是随机的“有序”列表,而不是随机的“生成”列表。 This is usually called "Shuffling". 这通常称为“混洗”。

Here's some sample code (not good code by any means, but I made it as close as yours as a sample) to achieve what you want: 这是一些示例代码(无论如何都不是好的代码,但是我使它与示例一样接近)来实现您想要的:

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);
}

The result should be something like: 结果应该是这样的:

4 9 2 1 3 7 5 8 6

Should work for any min and max values (generating as much numbers as there are in-between) 应该适用于任何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;
}

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

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