Jump to content
  • Advertisement


  • Content count

  • Joined

  • Last visited

Community Reputation

142 Neutral

About nightcracker

  • Rank

Personal Information

  1. nightcracker

    Simple and fast random number generator

    I forgot about this topic, and it has turned out to be very interesting! More people that are interested than I thought.   I thought I did it, but I forgot to link the paper of Tyche-i, which can be found here: http://eden.dei.uc.pt/~sneves/pubs/2011-snfa2.pdf
  2. Hey everyone,   I was not impressed with the random quality nor speed nor usability of the default rand() C function. So I took the best non-cryptographic RNG function I know of, and implemented it.   Here it is for your enjoyment! Usage is straightforward, speed varies between 5-12 times as fast than rand(), depending on the situation (inlining, register pressure, etc). Memory usage is low - only 16 bytes per RNG (you usually only need one).   #include <cstdint> #include <ctime> class FastRNG {     /* Implementation of the Tyche-i RNG by Samuel Neves and Filipe Araujo.        Period is expected to be 2^127, with a very good uniform distribution. */ public:     /* If no seed is passed to the constructor it will use the current time        in seconds as the seed. */     FastRNG(uint64_t seed = time(0)) {         a = seed << 32;         b = seed & 0xffffffffu;         c = 2654435769u;         d = 1367130551u;         for (int i = 0; i < 20; ++i) {             randint();         }     }     /* Returns a random integer between 0 and 2^32 - 1, inclusive. */     uint32_t randint() {         b = rotl32(b, 25) ^ c;         d = rotl32(d, 24) ^ a;         c -= d;         a -= b;         b = rotl32(b, 20) ^ c;         d = rotl32(d, 16) ^ a;         c -= d;         a -= b;         return b;     }     /* Returns a number between min and max, inclusive. Small statistical        biases might occur using this method - expected bias is < 2^-32. */     int32_t randrange(int32_t min, int32_t max) {         if (min > max) {             uint32_t t;             t = min;             min = max;             max = t;         }          return min + uint32_t((randint() * uint64_t(max - min + 1)) >> 32);     }     /* Returns a random double between 0.0 and 1.0 inclusive. */     double rand() {         return randint() * (1. / 4294967296.);     } private:     uint32_t a;     uint32_t b;     uint32_t c;     uint32_t d;     static inline uint32_t rotl32(uint32_t x, const int n) {         return (x << n) | (x >> (32 - n));     } };
  3. nightcracker

    Simple 2D game... Simple collision? Probably not? =-=

    You might want to publish this: [url="https://raw.github.com/gist/1460947/e94f96497ee759e64828a3e0b3ea7defb170a7b8/bitmask.c"]https://raw.github.c...0a7b8/bitmask.c[/url] [url="https://raw.github.com/gist/1460953/199aed3e3d6acf9353e25c1eb582c53eaa8777ea/bitmask.h"]https://raw.github.c...777ea/bitmask.h[/url] I ripped it out of pygame, it has very good performance. LGPL.
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!