Random Number Generators in C/C++

I found this article and thought that you guys might find it interesting because a number of you used calls to rand() in your last coursework. Basically it is comparing different implementations of pseudo random number generators in C and C++. It also gives a snippet of C++ (that you should easily be able to convert to C) that is both more random and faster than the standard version of rand().
#include <iostream>

// You need to initialize low and high to seed values.
// This would look better as a class but I’m lazy.
unsigned int low;
unsigned int high;

unsigned int GameRand()
{
    high = (high << 16) + (high >> 16);
    high += low;
    low += high;
    return high;
};

int main()
{
    high = 0xDEADBEEF;
    // usually I only have one uint32 to initialize with so I xor it
    //with a known good seed to get the second seed value
    low = high ^ 0x49616E42;
    for (int i = 0; i < 10; ++i)
    {
        std::cout << GameRand() << std::endl;
    }
}
Posted
Views