I can't figure this out

I am creating an ArrayList of Fish objects. I basically am building a flocking demo using XNA studio. I have a class called FishManager, and one called Fish. I want to add another class called FishGroup, but for now I have to figure out this problem. I am randomly creating the Fish's location and other properties including direction. I walk through the debugger and each fish has different values for everything except the ones I set. After I run the program when I go to draw the fish they all end up with the same values, it really doesn't make sense. I walk through a couple times with the debugger and each time more fish become alike. If someone can see what I'm doing wrong I would really appreciate it. I made sure that the array the constructor is only getting called once, but I am just lost to why I am having this problem.

[code]

namespace Flocking.Fish

{

classFishManager

{

privateTexture2D[] east_image =null;

privateTexture2D currentTexture =null;

privateArrayList group1 =null;

staticbool FishLoaded =false;

//This class should only be created once

public FishManager(GraphicsDevice device)

{

FishLoaded = LoadFish(device);

//Create Fish

group1 =newArrayList();

Fish tempFish;

for (int i = 0; i < 10; i++)

{

group1.Add(newFish());

}

}

privatebool LoadFish(GraphicsDevice device)

{

east_image =newTexture2DMusic;

east_image[0]=Texture2D.FromFile(device,"Sprites/ball fish e0000.bmp");

east_image[1] =Texture2D.FromFile(device,"Sprites/ball fish e0001.bmp");

east_image[2] =Texture2D.FromFile(device,"Sprites/ball fish e0002.bmp");

east_image[3] =Texture2D.FromFile(device,"Sprites/ball fish e0003.bmp");

east_image[4] =Texture2D.FromFile(device,"Sprites/ball fish e0004.bmp");

east_image[5] =Texture2D.FromFile(device,"Sprites/ball fish e0005.bmp");

east_imageDevil =Texture2D.FromFile(device,"Sprites/ball fish e0006.bmp");

east_image[7] =Texture2D.FromFile(device,"Sprites/ball fish e0007.bmp");

returntrue;

}

//This function draws the east images without rotation

publicvoid DrawFish(refSpriteBatch sprite)

{

foreach (Fish ain group1)

{

int index = a.getFrame();

if(index <= 0 || index > 8)

index = 1;

tempX += 35;

tempY += 35;

Vector2 tempPosition = a.getPosition();

sprite.Draw(east_image[index], tempPosition,newRectangle(0, 0, 64, 64),Color.TransparentWhite, a.getRotation(),newVector2 (32, 32), 0.75f,SpriteEffects.None, 1.0f);

}

}

}

classFish

{

privateVector2 direction;

privatefloat speed;

privatefloat MaxSpeed = 25;

privatefloat DesiredSpeed = 10;

privatefloat senseRange = 100;

privatefloat seperationDistance = 20;

privatefloat currentRotation = 0.0f;

privateVector2 position;

privateint currentFrame = 1;

public Fish()

{

Random rand =newRandom();

int xValue = rand.Next(25);

int yValue = rand.Next(25);

direction =newVector2(xValue, yValue);

direction.Normalize();

determineRotation();

speed = rand.Next((int)MaxSpeed);

//Make sure fish spawn in the box from 200, 200 to 400, 400

int startX = rand.Next(200);

int startY = rand.Next(200);

position =newVector2(startX + 200, startY + 200);

}

[/code]

[6182 byte] By [Adam23] at [2007-12-25]
# 1
Your problem is that your random object is in the Fish class and you make all your fish right at once. So basically, all your fish are getting created in the same tick, which equates to the same "random" start position.

That's why when you step through the code in the debugger, you see different values for all of them. They have then been created in separate ticks so get "truer" random numbers.

You have a couple of options to fix this. One way would be to seed the Random object with something more unique than tick for each of the fish. Another would be to create the random object at more of a global level so that the .Next would be working off the same random object. You could also make the game sleep for a bit before creating the next fish, this would allow time for it to move to the next tick.

Hope that helps!

GeorgeClingerman at 2007-10-8 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Game Studio Express...
# 2

Thank you for replying that helps a lot. I will start trying some of your suggestions.

Thanks again

Adam

Adam23 at 2007-10-8 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Game Studio Express...