aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/Math/je_random.cpp
blob: 216fd79d0c6f4658ecddd3d9a1102d9242f02ee0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <math.h>
#include "je_random.h"

namespace JinEngine
{
    namespace Math
    {

        RandomGenerator::RandomGenerator(uint32 seed)
            : mSeed(seed)
        {
        }

        uint32 RandomGenerator::rand()
        {
            unsigned int next = mSeed;
            uint32 result;

            next *= 1103515245;
            next += 12345;
            result = (unsigned int)(next / 65536) % 2048;

            next *= 1103515245;
            next += 12345;
            result <<= 10;
            result ^= (unsigned int)(next / 65536) % 1024;

            next *= 1103515245;
            next += 12345;
            result <<= 10;
            result ^= (unsigned int)(next / 65536) % 1024;

            mSeed = next;

            return result;
        }

        uint32 RandomGenerator::rand(uint32 min, uint32 max)
        {
            uint32 n = rand();
            return n % (max - min + 1) + min;
        }

        float RandomGenerator::randf(float min, float max, int ac)
        {
            uint32 a = pow(10.f, ac);
            uint32 n = rand(min*a, max*a);
            return (float)n / a;
        }

    }
}