1

I am trying to create a range of non duplicate random numbers between 1 - 10, I planned on doing this by storing each random number I made in to an array and then checking that array every time to make sure I ain't already used the number.

My problem is that instead of creating different random numbers such as 1, 2, 3 I just keep getting the same random number over and over.

        randomNumber();
        Label1.Text = randomRow + "";
        randomNumber();
        Label2.Text = randomRow + "";
        randomNumber();
        Label3.Text = randomRow + "";

        public int randomNumber()
        {
        List<int> numbers = new List<int>();
        int num = 0;
        Random randNum = new Random();
        num = randNum.Next(1, 11);

        if (numbers.Contains(num))
        {
            num = randNum.Next(1, 11);
        }
        else
        {
            randomRow = num;
            numbers.Add(num);
        }

        return randomRow;
    }
DanBarber
  • 165
  • 3
  • 15
  • 1
    You need to seed the random number: Randomd rnd = new Random(DateTime.Now.Millisecond) for example – Mike Cheel Dec 07 '13 at 14:51
  • 1
    Don't create a new Random instance everytime. Create it once and save it in a field. – Ufuk Hacıoğulları Dec 07 '13 at 14:51
  • 1
    Also, the `numbers` list will only exist inside `randomNumber()`, and each call will have "its own" List. Are you sure this is what you want? Another thing: You only generate a new random number if the generated one already exists. You don't check the newly generated one. – ChrisK Dec 07 '13 at 14:54
  • 1
    Not an explanation to your problem, but you may want to consider just creating a new array from 1 to 10 and shuffle it instead. That way, you'll get all numbers from 1-10 in a random order, which afaik is the same as you're trying to do; `var random = new Random(); var array = Enumerable.Range(1, 10).OrderBy(x => random.NextDouble()).ToArray();` – Joachim Isaksson Dec 07 '13 at 15:05

2 Answers2

3

Problem : everytime you are creating the RandomNumber object in too close time.

When you create a Random object, it's seeded with a value from the system clock. If you create Random instances too close in time, they will all be seeded with the same random sequence.

From Here

When you create a Random object, it's seeded with a value from the system clock. If you create Random instances too close in time, they will all be seeded with the same random sequence.

Solution :

move Random randNum = new Random(); outside the function randomNumber().

Try This:

Random randNum = new Random();
public int randomNumber()
    {
    List<int> numbers = new List<int>();
    int num = 0;

    num = randNum.Next(1, 11);

    if (numbers.Contains(num))
    {
        num = randNum.Next(1, 11);
    }
    else
    {
        randomRow = num;
        numbers.Add(num);
    }

    return randomRow;
}
Community
  • 1
  • 1
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
  • 1
    You should also post that why this will work and not other? – Rohit Vats Dec 07 '13 at 14:53
  • @RohitVats: Thank for your valuable comment, added reference. – Sudhakar Tillapudi Dec 07 '13 at 14:58
  • moving out the creation of Random number is not required absolutely, even inside the function it will give separate results – tariq Dec 07 '13 at 14:59
  • @tariq: if the method is called in a loop it can generate the duplicates,giving delay can help without moving the statement outside the function. – Sudhakar Tillapudi Dec 07 '13 at 15:02
  • It is probably worth noting that Random is actually pseudo random, the same seed will produce the same results. Instantiating the Random with a seed is advised. For example, Random randNum = new Random(Date.Now.GetHashCode()); – cstick Dec 07 '13 at 15:35
0

My best gues is that you are using this in a loop. In this case because you declare

Random randNum = new Random();

evry time this will generate tha same number. Just declare it BEFORE the loop and it should be fine.

Also you should consider a different approch because it is not a good practice. Like:

    int[] array = {1,2,3,4,5,6,7,8,9,10};
    Random randNum = new Random();
    int rand=0;
    int temp;
    for(int i = 0; i<10;i++)
    {
         rand = randNum.next(1,10-i);
         temp=array[rand];
         array[rand]=array[9-i];
         array[9-i]=temp;
    }
PeterRing
  • 1,767
  • 12
  • 20