Generate random numbers for every class instance

I'm trying out some AI techniques but I'm having a problem generating a TRULY random number. I create 20 instances of an ant class that should move and change directions on a random basis. They are doing that, but all in unison instead of each changing directions at different times. Its like synchronized ants. Anyone know how to create a truly random number each time the ants use this method

Random r = new Random();

if (r.Next(100) < 5)

ant.Speed = new Microsoft.Xna.Framework.Vector3(-ant.Speed.X, ant.Speed.Y, ant.Speed.Z);

if (r.Next(500) < 2)

ant.Speed = new Microsoft.Xna.Framework.Vector3(ant.Speed.X, -ant.Speed.Y, ant.Speed.Z);



Answer this question

Generate random numbers for every class instance

  • Zombie_002

    Thanks kindly, George. I don't want to go with a global variable because this method is within the "Wander" state class and it would be messy to pass in another variable. The ant manager sounds like the best, and it will probably make collission detections easier too.

    Can you put together some quick code on the best way to implement the ant manager


  • Titou

    what a simple and effective solution... Thanks bro
  • WidgetWorking

    George Clingerman wrote:
    You're going to want your Random object to be outside of your ant classes. Maybe within an Ant manager. Basically, what is happening is that when you create your Ant objects, they are created so quickly that your Random object is created in each of your classes at almost the same time. So, when you are moving through the "random" numbers, each random object is following the same random pattern.

    By moving the random object out of the classes and making every class use the same random object, you can guarantee that they random patterns they use will now be unique. I find that in most of my project, making one global random object for all of my classes helps out a lot when making sure my objects don't all respond in the same "random" pattern.

    So that's one route.

    Another thing you can do is create each random object in your ant classes with some kind of randomly generated seed. This would help them become different random objects and start them on different patterns.

    Either way should work and I hope that helps explain what was going on and give you some choices on where to go from here.




    Your solution is similar in thinking to what I did, and ultimately how I'd like to go about doing my stuff. Mind putting up a quick code example for either doing it in an Ant manager class, or perhaps would you mind telling me how you go about making a single global Random object I've tried making a single Random across my whole project before, but I can't seem to get it working correctly.

  • zoomer

    You're going to want your Random object to be outside of your ant classes. Maybe within an Ant manager. Basically, what is happening is that when you create your Ant objects, they are created so quickly that your Random object is created in each of your classes at almost the same time. So, when you are moving through the "random" numbers, each random object is following the same random pattern.

    By moving the random object out of the classes and making every class use the same random object, you can guarantee that they random patterns they use will now be unique. I find that in most of my project, making one global random object for all of my classes helps out a lot when making sure my objects don't all respond in the same "random" pattern.

    So that's one route.

    Another thing you can do is create each random object in your ant classes with some kind of randomly generated seed. This would help them become different random objects and start them on different patterns.

    Either way should work and I hope that helps explain what was going on and give you some choices on where to go from here.




  • Andy P.

    LOL, nothing like coming up with the easy solution. I'm not sure why I didn't think of that one either. Thanks dczraptor!


  • Michael Carty

    I might be able to give a bit of input here.

    I recently worked on an XNA remake of the classic Windows Starfield screen saver, and I ran into this exact problem.

    I had a Star class, and I maintained a list of stars, whereupon creation each star was given a random location.

    I realized that because the stars were getting created so quickly, the random number class was screwing up due to the system time probably being the same. Initially I had my class looking something like this (note that I'm typing this now, so it might be slightly incorrect);

    class Star
    {
    private Vector2 location;

    public Star()
    {

    Random rand = new Random();

    location = new Vector2( (float)rand.Next(640), (float)rand.Next(480) );

    }

    As I said, this was my old code. It looks fairly similiar to your current code.

    I was able to go ahead and get this working by doing the following:

    class Star
    {
    private static Random rand;
    private Vector2 location;

    public Star()
    {


    if (rand == null)
    rand = new Random();

    location = new Vector2( (float)rand.Next(640), (float)rand.Next(480) );

    }

    }

    Essentially what I ended up doing with my class was give it a static Random object. Since it's static, there's only one instance of the Random object across all instances of the class. My constructor checks to see if the Random object is null, which would occur if it wasn't initialized, and if it is it sets it to a new Random(). From here on out, you're just using that same Random class, so the Next() function is able to work properly.

    I don't know if that's the best way to go about getting a solution, but it worked really well for me. Another idea I was toying with was having my Game1 class have a Random member, and I'd just create a modified constructor for my Star class that accepted 2 values. I'd have Game1's Random object send 2 random numbers to every newly created Star class. This method worked, but I didn't like it as much as my final solution.

  • Steve Bostedor

    dczraptor wrote:

    Just a little minor detail: you could just do

    static Random rand = new Random();

    in the class declaration so you don't have to initialize it in the constructor. It doesn't rely on any xna or directx functions, so this will make your class easier to read.



    Oh jeez! I don't know why I didn't think of that when I came up with my solution. I guess I just thought it'd throw back some weirdo error or something, so I didn't try it. Thanks for that tip.

  • StephenMas

    Just a little minor detail: you could just do

    static Random rand = new Random();

    in the class declaration so you don't have to initialize it in the constructor. It doesn't rely on any xna or directx functions, so this will make your class easier to read.



  • Generate random numbers for every class instance