random

#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值