source: cpp/common/random.h @ 1005

Last change on this file since 1005 was 1005, checked in by Maciej Komosinski, 4 years ago

Higher conformance with C++17, but gave up after missing M_PI, M_PI_2, strdup() and more; other cosmetic improvements

  • Property svn:eol-style set to native
File size: 3.5 KB
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2018  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
4#ifndef _COMMON_RANDOM_H_
5#define _COMMON_RANDOM_H_
6
7#ifdef _MSC_VER
8#define NOMINMAX //we don't want Windows headers (minwindef.h) to define min() and max() as macros
9#undef min //for some reason, NOMINMAX did not work so we have to #undef anyway
10#undef max
11#endif
12
13#include <time.h> //time()
14#include <stdint.h> //uintptr_t
15#ifdef MULTITHREADED
16#include "threads.h"
17#endif
18#ifdef LINUX
19#include <unistd.h>
20#include <sys/stat.h>
21#include <fcntl.h>
22#endif
23#ifdef _WIN32
24#define _WINSOCKAPI_ //http://stackoverflow.com/questions/1372480/c-redefinition-header-files
25#include <rpc.h> //UUID
26#pragma comment(lib, "Rpcrt4.lib")
27#endif
28
29//adapted from
30//http://en.wikipedia.org/wiki/Mersenne_twister#Pseudocode
31//http://my.opera.com/metrallik/blog/2013/04/19/c-class-for-random-generation-with-mersenne-twister-method
32
33class RandomGenerator
34{
35private:
36        static const unsigned int length = 624;
37        static const unsigned int bitMask_32 = 0xffffffff;
38        static const unsigned int bitPow_31 = 1 << 31;
39        static const unsigned int MAXVALUE = 0xffffffff;
40        unsigned int counter; //only used in randomize(). uninitialized is OK
41#ifdef MULTITHREADED
42        pthread_mutex_t lock;
43#endif
44        unsigned int *mt;
45        unsigned int idx;
46public:
47
48        RandomGenerator(unsigned int seed)
49        {
50#ifdef MULTITHREADED
51                pthread_mutex_init(&lock, NULL);
52#endif
53                mt = new unsigned int[length];
54                setSeed(seed);
55        }
56
57        inline void setSeed(unsigned int seed)
58        {
59#ifdef MULTITHREADED
60                pthread_mutex_lock(&lock);
61#endif
62                idx = 0;
63                mt[0] = seed;
64                for (unsigned int i = 1; i < length; i++)
65                        mt[i] = (1812433253 * (mt[i - 1] ^ (mt[i - 1] >> 30)) + i) & bitMask_32;
66#ifdef MULTITHREADED
67                pthread_mutex_unlock(&lock);
68#endif
69        }
70
71        unsigned int randomize()
72        {
73                unsigned int seed;
74                //for ms visual, could use http://msdn.microsoft.com/en-us/library/sxtz2fa8.aspx
75#ifdef LINUX
76                int fd = open("/dev/urandom", O_RDONLY);
77                if (fd >= 0)
78                {
79                        read(fd, &seed, sizeof(seed));
80                        close(fd);
81                }
82                else
83#endif
84                {
85                        counter++;
86                        seed = (unsigned int)time(NULL);           //time (seconds); could use hi-res timer but then we would depend on common/timer.h
87                        seed ^= counter;                           //incremented value, possibly randomly initialized
88                        seed ^= (unsigned int)(uintptr_t)&counter; //memory address
89                }
90#ifdef _WIN32 //add more randomness from uuid
91                UUID uuid;
92                ::UuidCreate(&uuid);
93                seed ^= uuid.Data1 ^ uuid.Data2 ^ uuid.Data3 ^ uuid.Data4[0];
94#endif
95                setSeed(seed);
96                return seed;
97        }
98
99        inline unsigned int getUint32()
100        {
101#ifdef MULTITHREADED
102                pthread_mutex_lock(&lock);
103#endif
104                if (idx == 0) gen();
105                unsigned int y = mt[idx];
106                idx = (idx + 1) % length;
107#ifdef MULTITHREADED
108                pthread_mutex_unlock(&lock);
109#endif
110                y ^= y >> 11;
111                y ^= (y << 7) & 2636928640U;
112                y ^= (y << 15) & 4022730752U;
113                y ^= y >> 18;
114                return y;
115        }
116
117        //UniformRandomBitGenerator
118        typedef unsigned int result_type;
119        static constexpr unsigned int min() { return 0; }
120        static constexpr unsigned int max() { return MAXVALUE; }
121        inline unsigned int operator()() { return getUint32(); }
122
123        inline double getDouble() // [0,1)
124        {
125                return double(getUint32()) / ((int64_t)(MAXVALUE)+1);
126        }
127
128        inline void gen()
129        {
130                for (unsigned int i = 0; i < length; i++)
131                {
132                        unsigned int y = (mt[i] & bitPow_31) + (mt[(i + 1) % length] & (bitPow_31 - 1));
133                        mt[i] = mt[(i + 397) % length] ^ (y >> 1);
134                        if (y % 2) mt[i] ^= 2567483615U;
135                }
136                return;
137        }
138
139        ~RandomGenerator()
140        {
141                delete[] mt;
142        }
143};
144#endif
Note: See TracBrowser for help on using the repository browser.