#ifndef RANDOM_H
#define RANDOM_H

//#include <cstdlib>

#define PI 3.141592654f

namespace rnd
{
    extern long randomSeed;

    inline void seed(long s)
    {
        randomSeed = s;
    }

    // Taken from SPARK
    template<typename T>
    inline T random(T min, T max)
    {
        // optimized standard minimal
        long tmp0 = 16807L * (randomSeed & 0xFFFFL);
        long tmp1 = 16807L * (randomSeed >> 16);
        long tmp2 = (tmp0 >> 16) + tmp1;
        tmp0 = ((tmp0 & 0xFFFF)|((tmp2 & 0x7FFF) << 16)) + (tmp2 >> 15);

        // correction of the error
        if ((tmp0 & 0x80000000L) != 0)
            tmp0 = (tmp0 + 1) & 0x7FFFFFFFL;

        randomSeed = tmp0;

        // find a random number in the interval
        return static_cast<T>(min + ((randomSeed - 1) / 2147483646.0) * (max - min));
    }
}

#endif //RANDOM_H

