iFarbod

MT Wrapper

Mar 8th, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.67 KB | None | 0 0
  1. // Mersenne Twister Engine wrapper
  2. // Author(s):       iFarbod <[email protected]>
  3. //
  4. // Copyright (c) 2014-2016 San Andreas Online
  5. // Use of this source code is governed by the MIT license that can be
  6. // found in the LICENSE file.
  7.  
  8. #pragma once
  9. #include "types.hpp"
  10. #include "MT.hpp"
  11.  
  12. #ifdef _WIN32
  13. #include <Windows.h>
  14. #else // TODO: move this to a utility function
  15. #include <sys/time.h>
  16.  
  17. inline unsigned GetTickCount__mtrand()
  18. {
  19.     struct timeval tv;
  20.     if (gettimeofday(&tv, NULL) != 0)
  21.         return 0;
  22.  
  23.     return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
  24. }
  25.  
  26. #endif // _WIN32
  27.  
  28. class MTRand
  29. {
  30. private:
  31.     class _11213b
  32.     {
  33.     private:
  34.         MT11213b rnd;
  35.     public:
  36.         _11213b()
  37.         {
  38. #ifdef _WIN32
  39.             rnd.seed(GetTickCount());
  40. #else
  41.             rnd.seed(GetTickCount__subrand());
  42. #endif // _WIN32
  43.         }
  44.  
  45.         u32 Next()
  46.         {
  47.             return rnd();
  48.         }
  49.  
  50.         u32 Next(u32 max)
  51.         {
  52.             return Next() % max;
  53.         }
  54.  
  55.         // [min, max)
  56.         u32 Next(u32 min, u32 max)
  57.         {
  58.             return Next((max - min)) + min;
  59.         }
  60.  
  61.         // inclusive
  62.         // [min, max]
  63.         u32 Next_(u32 min, u32 max)
  64.         {
  65.             return Next((max - min) + 1) + min;
  66.         }
  67.  
  68.         bool NextBool()
  69.         {
  70.             return Next(2) != 0;
  71.         }
  72.     } _MT11213b;
  73.  
  74.     class _19937
  75.     {
  76.     private:
  77.         MT19937 rnd;
  78.     public:
  79.         _19937()
  80.         {
  81. #ifdef _WIN32
  82.             rnd.seed(GetTickCount());
  83. #else
  84.             rnd.seed(GetTickCount__subrand());
  85. #endif // _WIN32
  86.         }
  87.  
  88.         u32 Next()
  89.         {
  90.             return rnd();
  91.         }
  92.  
  93.         u32 Next(u32 max)
  94.         {
  95.             return Next() % max;
  96.         }
  97.  
  98.         u32 Next(u32 min, u32 max)
  99.         {
  100.             return Next((max - min)) + min;
  101.         }
  102.  
  103.         // inclusive
  104.         // [min, max]
  105.         u32 Next_(u32 min, u32 max)
  106.         {
  107.             return Next((max - min) + 1) + min;
  108.         }
  109.  
  110.         bool NextBool()
  111.         {
  112.             return Next(2) != 0;
  113.         }
  114.     } _MT19937;
  115.  
  116.     class _19937_64
  117.     {
  118.     private:
  119.         MT19937_64 rnd;
  120.     public:
  121.         _19937_64()
  122.         {
  123. #ifdef _WIN32
  124.             rnd.seed(GetTickCount64());
  125. #else
  126.             rnd.seed(GetTickCount__subrand());
  127. #endif // _WIN32
  128.         }
  129.  
  130.         u64 Next()
  131.         {
  132.             return rnd();
  133.         }
  134.  
  135.         u64 Next(u64 max)
  136.         {
  137.             return Next() % max;
  138.         }
  139.  
  140.         u64 Next(u64 min, u64 max)
  141.         {
  142.             return Next((max - min)) + min;
  143.         }
  144.  
  145.         // inclusive
  146.         // [min, max]
  147.         u64 Next_(u64 min, u64 max)
  148.         {
  149.             return Next((max - min) + 1) + min;
  150.         }
  151.  
  152.         bool NextBool()
  153.         {
  154.             return Next(2) != 0;
  155.         }
  156.     } _MT19937_64;
  157.  
  158.     //
  159.     // Instance getter functions
  160.     //
  161. public:
  162.     inline decltype(_MT11213b)& GetMT11213b()
  163.     {
  164.         return _MT11213b;
  165.     }
  166.  
  167.     inline decltype(_MT19937)& GetMT19937()
  168.     {
  169.         return _MT19937;
  170.     }
  171.  
  172.     inline decltype(_MT19937_64)& GetMT19937_64()
  173.     {
  174.         return _MT19937_64;
  175.     }
  176.  
  177.     // -----------------------------------------
  178.     // --------------- Singleton ---------------
  179.     // -----------------------------------------
  180. public:
  181.     static MTRand& GetInstance()
  182.     {
  183.         static MTRand instance;
  184.         return instance;
  185.     }
  186. private:
  187.     MTRand()
  188.     {}
  189.  
  190. public:
  191.     MTRand(MTRand const&) = delete;
  192.     void operator=(MTRand const&) = delete;
  193.  
  194. };
  195. static MTRand& mtrand = MTRand::GetInstance(); // for easy use
Advertisement
Add Comment
Please, Sign In to add comment