Random
is seeded by the clock; you might want to do something like:
Random rand1 = new Random();
Random rand2 = new Random(rand1.Next());
Task firstRacer = Task.Run(() => Race(racerOne, rand1));
Task secondRacer = Task.Run(() => Race(racerTwo, rand2));
private void Race(Slider racer, Random rand)
{
int step = 0;
while (step < 1000)
{
step += rand.Next(0, 10);
Dispatcher.BeginInvoke(new ThreadStart(() => racer.Value = step));
Thread.Sleep(rand.Next(0, 300));
}
}
This creates two separate Random
instances with different seeds (by using the 1st to seed the second), and then passes these into the Race
as arguments. This removes any risk of overly-predictable behaviour due to timing.