1
for (int i = 1; i < 10; i++)
{
  number = randomNumber();
  if (!straightLine.Contains(number))
  {
     straightLine.Add(number);
  }

}

public static int randomNumber()
{
    Random random = new Random();
    return random.Next(1, 100);
}

when I debug it works fine but when I run the program it gets 1 random number and that's it. so the problem is that the random Number method is only called once (when I don't debug then it calls it every time)

what can I do?

Ajith
  • 1,447
  • 2
  • 17
  • 31
simbasang
  • 11
  • 1
  • yeah i tried everything printing the whole loop and when i debug it works as i want it to work els it just get one number the first time and sticks to it :( – simbasang Feb 01 '20 at 12:57

2 Answers2

1

germi's answer is slightly misleading as it implies that any repeated instantiation of a new Random will produce the same sequence of values. This isn't quite correct, because your debug code works as you expect.

The documentation for Random says that the Random number generator is seeded from the system clock if you dont pass a seed value. https://learn.microsoft.com/en-us/dotnet/api/system.random.-ctor?view=netframework-4.8

The reason it works in debug is that debugging code is very slow (you're literally taking hundreds of milliseconds to step through the code a line at a time) and the clock has time to change in between runs of the loop.

When your code is run at full speed it runs so quickly that the system clock simply won't have changed to a new milliseconds value, so your repeated making of a new Random will result in it being seeded with the same value from the clock

If you were to insert some delay in the code such as Thread.Sleep(1000) in the loop then it would work. If you were to run the loop a million times it would take long enough to work through that the clock would eventually change - a million iterations might take long enough for a small handful of values to come out of the Random

The recommendation for a solution is sound though; make one new Random somewhere outside of the loop and then repeatedly call it. You could also seed the Random with something that is unique each time (like the value of i), though you should bear in mind that providing a particular seed will guarantee that the same random number comes out of it when you call Next. In some contexts this is useful, because you might want a situation where you can provide a certain value and then see the same sequence of random numbers emerge. The main thing to be mindful of is that by default the Random starts it's sequence based on the clock; two different computers with different time settings can theoretically produce the same random numbers if their clocks are reading the same at the moment the Random is created.

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
0

Repeatedly instantiating a new Random instance will lead to the same number being generated, since the seed will be the same.

The solution is to have one instance of Random in the class that generates the values:

var random = new Random();
for (int i = 1; i < 10; i++)
{
    number = random.Next(1,100);
    if (!straightLine.Contains(number))
    {
        straightLine.Add(number);
    }
}

Note that this behavior only exists in .NET Framework, .NET Core will produce different values even across multiple Random instances created in quick succession.

germi
  • 4,628
  • 1
  • 21
  • 38