0

I use the Random() function in order to get random double values. It works fine when launched manually (each time new random values are well generated), but the values are the same each time when it is launched by the Windows Task Scheduler.

Would you know why and how to resolve this issue ? Thanks

    var random = new Random(DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Hour);

    double randomStat = new double();

    randomStat = Math.Round(GetRandomDouble(random, 5, 250), 1);


static double GetRandomDouble(Random random, double min, double max)
{
     return min + (random.NextDouble() * (max - min));
}
Liam
  • 27,717
  • 28
  • 128
  • 190
  • 1
    There is nothing special about the task scheduler, it just runs whatever you tell it to. It's really not clear how you run this code though. We'll need a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) of your problem – Liam Dec 03 '18 at 11:20
  • 4
    how often is the `new Random(...)` being invoked here? with the code as shown, any instances created in the same second will always have the same seed; also, anything *reliably* launched at the same time each day will have the same seed... you really aren't giving yourself many seed options here... about 144 different possibilities? – Marc Gravell Dec 03 '18 at 11:20
  • 1
    Could it be that the same seed is used? By default the DateTime is used as seed, wich at least gives you differences along the milliseconds. But you code only resolves down to seconds. – Christopher Dec 03 '18 at 11:21
  • @Christopher Actually, DateTime only changes every 20ms or so, so it's not quite milliseconds. – Matthew Watson Dec 03 '18 at 11:22
  • 1
    It is launched everyday at 7pm... So yes the seed is always the same ! That might be the issue... But in this case how to get a new seed each time ? Thanks –  Dec 03 '18 at 11:22
  • @MatthewWatson: True. Still a lot more often then something based only on Seconds. – Christopher Dec 03 '18 at 11:22
  • 3
    @InvestDataSystemsFR "how to get a new seed each time ?" that's simple... just let `Random` do it internally; heck, a `new Random()` that you store somewhere (perhaps just a static field) and query `Next*` from repeatedly would work fine; if you have multiple threads, then add some `lock` love – Marc Gravell Dec 03 '18 at 11:23
  • @Christopher absolutely! I was just being complete. :) – Matthew Watson Dec 03 '18 at 11:24
  • This might helps you https://stackoverflow.com/questions/1785744/how-do-i-seed-a-random-class-to-avoid-getting-duplicate-random-values – Hamza Haider Dec 03 '18 at 11:25
  • Thank you all, And also Hamza, I'll use the following technique for the seed : Guid.NewGuid().GetHashCode() –  Dec 03 '18 at 11:26

1 Answers1

0

True randomness is not really possible in computers. Instead we use pseudo-random number generators.

The rules is that given the same seed and the same sequence of orders, the pseude-RNG will always return the exactly same results.

By default Random will implicitly retreive a seed value based on the local DateTime. As that is a derviate of Unix Time (every tick since moment X), you will only get duplicates in rare cases (creating instances milliseconds appart, a common cause of issues with Random).

What you do is give it the seed value based on hour, minute and second. And 7:00:00 PM today has the same values here as 7:00:00 PM tomorrow.

Christopher
  • 9,634
  • 2
  • 17
  • 31