0
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;

`

9
  • 1
    I don't think they are repeating. The code is doing what it should, you are just being "unlucky" with your generated numbers. If the repeating is constant, then there's something else that you are not showing us. By the way, there's no point on having a random generator generate a seed for another random generator.
    – Jcl
    Jan 5, 2015 at 11:34
  • Numbers should be allowed to repeat in a random number sequence. If you were sure that numbers did not repeat then the sequence would not have desirable random properties.
    – Jonny
    Jan 5, 2015 at 11:36
  • Ah, I see now your very last sentence... let me write an answer
    – Jcl
    Jan 5, 2015 at 11:36
  • Do you want 1-10 in a random order?
    – Jonny
    Jan 5, 2015 at 11:38
  • yeah.. i want to make a prng with 10 different number. like 1 5 3 7 2 9 4 6 8 10. so the number isnt repeated. any idea? it just a simple case.. so i can make a 10, 15, 20, 50, 100 random numbers without repeating it. Jan 5, 2015 at 11:39

2 Answers 2

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

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)

5
  • i appreciate what you do.. i mark it as a best answer. the one i need is i have a seed, that will generate as much number as i want without repeating same number. so with the same seed, i can retrieve the same number (PRNG). my further work, i will use it as image color coordination to hide a bit message in color. Jan 5, 2015 at 12:00
  • As a warning: this code is far from optimal... I wrote it as "explanational" and not as optimal as possible, and has some quirks (for example: if you call Shuffle repeatedly it'll probably use the same seed, I'd put a random seed as a parameter to the function). You can ask if you have any further problems with it or have any trouble understanding it.
    – Jcl
    Jan 5, 2015 at 12:02
  • For your requirement, you could pass a seed parameter to the Shuffle function (which in turn would use it as a seed on the inner Random object) and the list would always be shuffled the same way for the same seed.
    – Jcl
    Jan 5, 2015 at 12:05
  • As a side note: for your next SO questions, please ask specifically what you want, and you might get better answers, otherwise you are doing a XY problem question. Check the answers on this post: meta.stackexchange.com/questions/66377/what-is-the-xy-problem for clarification on what this is.
    – Jcl
    Jan 5, 2015 at 12:08
  • thanks for the help.. i am trying to work with the seed. and i will consider your side note. Jan 5, 2015 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.