Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Mersenne Twister Engine wrapper
- // Author(s): iFarbod <[email protected]>
- //
- // Copyright (c) 2014-2016 San Andreas Online
- // Use of this source code is governed by the MIT license that can be
- // found in the LICENSE file.
- #pragma once
- #include "types.hpp"
- #include "MT.hpp"
- #ifdef _WIN32
- #include <Windows.h>
- #else // TODO: move this to a utility function
- #include <sys/time.h>
- inline unsigned GetTickCount__mtrand()
- {
- struct timeval tv;
- if (gettimeofday(&tv, NULL) != 0)
- return 0;
- return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
- }
- #endif // _WIN32
- class MTRand
- {
- private:
- class _11213b
- {
- private:
- MT11213b rnd;
- public:
- _11213b()
- {
- #ifdef _WIN32
- rnd.seed(GetTickCount());
- #else
- rnd.seed(GetTickCount__subrand());
- #endif // _WIN32
- }
- u32 Next()
- {
- return rnd();
- }
- u32 Next(u32 max)
- {
- return Next() % max;
- }
- // [min, max)
- u32 Next(u32 min, u32 max)
- {
- return Next((max - min)) + min;
- }
- // inclusive
- // [min, max]
- u32 Next_(u32 min, u32 max)
- {
- return Next((max - min) + 1) + min;
- }
- bool NextBool()
- {
- return Next(2) != 0;
- }
- } _MT11213b;
- class _19937
- {
- private:
- MT19937 rnd;
- public:
- _19937()
- {
- #ifdef _WIN32
- rnd.seed(GetTickCount());
- #else
- rnd.seed(GetTickCount__subrand());
- #endif // _WIN32
- }
- u32 Next()
- {
- return rnd();
- }
- u32 Next(u32 max)
- {
- return Next() % max;
- }
- u32 Next(u32 min, u32 max)
- {
- return Next((max - min)) + min;
- }
- // inclusive
- // [min, max]
- u32 Next_(u32 min, u32 max)
- {
- return Next((max - min) + 1) + min;
- }
- bool NextBool()
- {
- return Next(2) != 0;
- }
- } _MT19937;
- class _19937_64
- {
- private:
- MT19937_64 rnd;
- public:
- _19937_64()
- {
- #ifdef _WIN32
- rnd.seed(GetTickCount64());
- #else
- rnd.seed(GetTickCount__subrand());
- #endif // _WIN32
- }
- u64 Next()
- {
- return rnd();
- }
- u64 Next(u64 max)
- {
- return Next() % max;
- }
- u64 Next(u64 min, u64 max)
- {
- return Next((max - min)) + min;
- }
- // inclusive
- // [min, max]
- u64 Next_(u64 min, u64 max)
- {
- return Next((max - min) + 1) + min;
- }
- bool NextBool()
- {
- return Next(2) != 0;
- }
- } _MT19937_64;
- //
- // Instance getter functions
- //
- public:
- inline decltype(_MT11213b)& GetMT11213b()
- {
- return _MT11213b;
- }
- inline decltype(_MT19937)& GetMT19937()
- {
- return _MT19937;
- }
- inline decltype(_MT19937_64)& GetMT19937_64()
- {
- return _MT19937_64;
- }
- // -----------------------------------------
- // --------------- Singleton ---------------
- // -----------------------------------------
- public:
- static MTRand& GetInstance()
- {
- static MTRand instance;
- return instance;
- }
- private:
- MTRand()
- {}
- public:
- MTRand(MTRand const&) = delete;
- void operator=(MTRand const&) = delete;
- };
- static MTRand& mtrand = MTRand::GetInstance(); // for easy use
Advertisement
Add Comment
Please, Sign In to add comment