简体   繁体   中英

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#. 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

from the result, the number 3,7,9 are repeating. 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 :

        // 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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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