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