TIGSource Forums

Developer => Technical => Topic started by: Brian Wilbur on September 24, 2011, 05:29:33 PM



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.

  • The carrier object has a Random object for randomizing the IDs of the items it contains.
  • These IDs (simply ints) are placed into an int[5] which houses them until they're ready for use.
  • When the carrier object is opened, the items inside drop to the ground. It's at this point that each item is spawned.
  • The carrier object loops through the array at this point and adds 5 new Item objects into a List of those objects in the Main class, based on the IDs stored in the array.
  • If the ID at the current position in the array is 5, no object gets added. If it's 0-4, it adds the object with that ID.
  • When each new Item() is created, it has it's own Random() object.
  • I then call random.Next(int, int) on that object twice during initialization to assign it a random horizontal speed and vertical speed based on four variables
  • Issue - All 5 objects that spawn travel at the same horizontal/vertical speeds.
  • Issue - All container objects, when broken, spawn the same 5 seeds.
  • Issue - I tried to use Thread.Sleep() to circumvent the problem -- It worked, but it's not really the appropriate way to do it...right?

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.