XNA Creators Club Online
Page 1 of 1 (5 items)
Sort Posts: Previous Next

Random Number Generates Diagonal Line

Last post 9/24/2009 4:44 PM by Shawn Hargreaves. 4 replies.
  • 9/24/2009 1:33 AM

    Random Number Generates Diagonal Line

    Using the following:

                            minX = randX.Next(0, Game.GraphicsDevice.Viewport.Width);
                            minY = randY.Next(0, Game.GraphicsDevice.Viewport.Height);

    I get a perfectly straight diagonal line for my enemy spawn.

    What I'd like to achieve is random location across the viewport height and width
  • 9/24/2009 1:45 AM In reply to

    Re: Random Number Generates Diagonal Line

    I think that error is somewhere else, not taking random values, because this should work just a way you wanted. Debug your code and check values, that is easiest way to check that everything working a way you wanted.
    FontBuilder TrueType Font Editor
  • 9/24/2009 1:48 AM In reply to

    Re: Random Number Generates Diagonal Line

    Random numbers are not really random.  They are calculated from a base seed with an equation that will result in a normal or near normal distrubution.  The equation is always the same in any given implementation and so if you seed the random number generator with the same seed, you will always get exactly the same sequence of numbers.

    Having said that, you are using two random number generators in parallel.  I suspect that you initialize both of them at the same time.  So, when you make the calls, you are getting the same numbers internally.  The random number generator deals with large numbers and uses them to create a percentage which is then applied to your limits.  So, the same percentage on your screen coordinates every call will create a diagonal line.

    Short answer:  You should almost always just deal with a single number generator and initialize it once.
  • 9/24/2009 1:51 AM In reply to

    Re: Random Number Generates Diagonal Line

    Answer
    Reply Quote
    Ah, I completely forgot, I'm using a MATRIX camera, so I needed to minus my Camera's Position to the Vector.
  • 9/24/2009 4:44 PM In reply to

    Re: Random Number Generates Diagonal Line

    What are the objects randX and randY? Do you have two different instances of the Random class? (you typically only want one Random instance).
    XNA Framework Developer - blog - homepage
Page 1 of 1 (5 items) Previous Next