random

来源:互联网 发布:宁夏4.14杀人案知乎 编辑:程序博客网 时间:2024/05/21 09:10
#ifndef RNG_H#define RNG_H#include <memory>#include <random>namespace rng{using namespace std;const static size_t MAXRAND = random_device::max();class RNG{private:random_device m_rd;shared_ptr<mt19937>m_regen;shared_ptr<uniform_int_distribution<size_t> >m_dist;RNG() noexcept;public:static RNG &getInstance();size_t getInteger() noexcept;};size_t getRand(size_t r);}#endif#include "RNG.h"namespace rng{RNG::RNG() noexcept{m_regen = make_shared<mt19937>(m_rd());m_dist = make_shared<uniform_int_distribution<size_t> >(0, MAXRAND);}RNG & RNG::getInstance(){static RNG instance;return instance;}size_t RNG::getInteger() noexcept{return (*m_dist)(*m_regen);}size_t getRand(size_t r){return RNG::getInstance().getInteger() % r;}}/****************************************************************************@File Name: test.cpp@Author: binwang@mail: binwang@trendmicro.com.cn@Created Time: Sat 04 Feb 2017 12:58:48 AM CST****************************************************************************/#include <iostream>#include "RNG.h"using namespace rng;int main(int argc, char **argv){for(int i = 0;i < 10;i++) cout << getRand(1000) << endl;return 0;}CC=g++all:$(CC) -std=c++11 -g -o test test.cpp RNG.h RNG.cpp

0 0
原创粉丝点击