Hey,

I have a class called Greyhounds.

In the variable declarations, I have:

public Random randomizer = new Random();

I know how to use random.next and everything. Here's where the problem is. The greyhounds class has a method Run().

public bool Run()
{
int moveForward = randomizer.Next(4) + 1;
currentLocation = MyPictureBox.Location;
currentLocation.X += moveForward; // .X because currentLocation is of type "Point". 
MyPictureBox.Location = currentLocation; 
// There's more but it's irrelevant

Anyways, I have four instances of the object. grey1, grey2, grey3, and grey4. The problem comes when I call all of their Run() methods. They all generate the same random number. The code in Run() is supposed to move the pictures across the screen in random intervals, but all of the instances of the object move at the same random interval so they're always collinear. It's as if they're all feeding from the same random number once, changing random numbers, then feeding on that.

So how do I write the method so they don't all go at the same interval?

Thanks,
Zephyr-

Make randomizer a static class variable instead of an instance variable. What is happening is that all the generators are being created so fast that the seed value, which is based off the time, is the same. This causes them to all generate the same sequence. If you just have one random generator, you won't have this problem.

Alternatively you could use a Random constructor with a different integer seed value.
public Random randomizer = new Random(seedGrey1);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.