|
Title: Randoms are driving me insane! (C#, XnA) Post by: Brian Wilbur on September 24, 2011, 05:29:33 PM Hey guys, bit of a newbie programmer here (about 2 years of exp. with C# and I have dabbled in other languages)...for some reason, one particular instance of my Randoms is driving me crazy.
I'm using the XnA framework, also. Basically I have an object which "carries" other objects in the game environment.
Code: <When you punch the Carrier object && it's not broken yet> { for (int slot = 0; slot < 5; slot++) { if (cacheContents[slot] != 5) Game1.Instance.seedItems.Add(new SeedItem(cacheContents[slot], new Rectangle(position.X, position.Y, 96, 96))); } cacheBroken = true; } Code: public class Item { Random random = new Random(); Rectangle position; int hSpeed_min; int hSpeed_max; int vSpeed_min; int vSpeed_max; float hSpeed; float vSpeed; public class Item(int newType, Rectangle newPosition) { hSpeed_min = -9; hSpeed_max = 10; hSpeed = random.Next(hSpeed_min, hSpeed_max); vSpeed_min = 0; vSpeed_max = 5; vSpeed = random.Next(vSpeed_min, vSpeed_max); } } Forgive me if the code is a bit weird. I'm working from memory. The absolute ONLY way I can seem to get this to work is if I call a System.Threading.Thread.Sleep() between each call, but that can't be the most efficient way to do it...right? I have plenty of other randomization code in this program that works just like this and it works fine. Don't understand what the problem is. Title: Re: Randoms are driving me insane! (C#, XnA) Post by: Brian Wilbur on September 24, 2011, 06:17:39 PM Finally think I got it -- I wrote a little snippet which "staggers" the items spawning from the cache. Essentially there is a variable wait time (but without using Thread.Sleep()) between the spawn of each seed. It seems that they're going in the right direction now.
Would still like to see thoughts on this matter & strategies though, as the other half of my problem (the actual ID assigning in the initial array) is still requiring a Sleep command. Title: Re: Randoms are driving me insane! (C#, XnA) Post by: moi on September 24, 2011, 07:17:35 PM don't put a Random object in each carrier object but use a global (static?) Random object for all objects, I don't remember the details but it's an issue like that. It's a common mistake done with C#, google it.
Title: Re: Randoms are driving me insane! (C#, XnA) Post by: JOBA on September 24, 2011, 07:20:01 PM don't put a Random object in each carrier object but use a global (static?) Random object for all objects, I don't remember the details but it's an issue like that. It's a common mistake done with C#, google it. That's correct. Make random object static within the item class. |