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

Random Number Generator

Last post 17/11/2009 6:16 by jwatte. 7 replies.
  • 07/11/2009 23:00

    Random Number Generator

    Hi im new to c# and im trying to get the random number generator working, in c++ its done differently and im having trouble finding out how to do it in c# the code the get the variable is:
    System.Random RandomEnemyLocationX = new Random(); 
    int EnemyLocationX = RandomEnemyLocationX.Next(0,800); 
    please help thanks :)
  • 07/11/2009 23:02 In reply to

    Re: Random Number Generator

    Answer
    Reply Quote
    You have the right code, however every time you make a new Random() it will seed it with the current timer. This measn if you call it fast enough you will never get random numbers becuase they will all be seeded the same.

    so move the new Random() to a static property on your class and then just call .Next (or one if its variants) as needed


    class foo
    {
    static Random r = new Random();
    int x;
    void setPosition()
    {
     x = r.Next(0, 800);
    }
    }
    Play Kissy Poo - a game for 4 year olds on Xbox and windows
    The ZBuffer
    News and information for XNA
      Follow The Zman on twitter, Email me
        Please read the forum FAQs - Bug/Feature reporting
          Don't forget to mark good answers and good playtest feedback when you see it!!!
  • 07/11/2009 23:04 In reply to

    Re: Random Number Generator

    Thanks alot :)
  • 07/11/2009 23:51 In reply to

    Re: Random Number Generator

    I found a really good RNG, and I improved it a little, so it can give you floats. Enjoy!

    http://pastebin.com/f67bcb04d
    This is the spot where I'm supposed to identify myself as a unique individual, right?
  • 08/11/2009 0:17 In reply to

    Re: Random Number Generator

    cable729:
    I found a really good RNG, and I improved it a little, so it can give you floats.
    System.Random can already give you floats...
    class foo 
        static Random rand = new Random(); 
     
        public void MyFunction() 
        { 
            float myRandFloat = (float)rand.NextDouble() * (maxValue - minValue) + minValue; 
        } 

  • 16/11/2009 21:52 In reply to

    Re: Random Number Generator

    I prefer Cryptography.RNGCryptoServiceProvider over the System.Random class. I'm not sure how it works, but when Random will give the same number (multiple numbers in the same cycle) RNGCryptoServciceProvider does not. Maybe I was just using Random wrong.
    Freefall Game Engine - In Development
  • 16/11/2009 22:01 In reply to

    Re: Random Number Generator

    ShadowyCore:
    I prefer Cryptography.RNGCryptoServiceProvider over the System.Random class


    The RNGCryptoServiceProvider is not available on the Xbox, so that limits its usefulness.

    ShadowyCore:
    Maybe I was just using Random wrong.


    Maybe, its easy to do by creating a new Random() when you should be reusing one. There can also be issues when using a single Random instance across threads. But for the most part, Random will be sufficient.
    www.dadoogames.com
    Curling 2010 - in playtest soon, this month or next, this year for sure (maybe)
  • 17/11/2009 6:16 In reply to

    Re: Random Number Generator

    I prefer Cryptography.RNGCryptoServiceProvider over the System.Random class


    It's great to seed other random number generators, but all the good implementations are orders of magnitude slower than your typical shift register, hash-based or linear congruential random number generator (not to mention the Mersenne Twister, the current champ in amortized speed and sequence length).

    Sadly, Crypto is not available on the Xbox, though, so you can't even use it to seed your other RNG :-(

    Jon Watte, Direct3D MVP
    Tweets, occasionally
    kW X-port 3ds Max .X exporter
    kW Animation source code
Page 1 of 1 (8 items) Previous Next