怎样在c语言里生成随机偶数数,如何在C ++中生成随机数?

如果您使用的是升压库,则可以通过以下方式获取随机生成器:

#include

#include

// Used in randomization

#include

#include

#include

#include

using namespace std;

using namespace boost;

int current_time_nanoseconds(){

struct timespec tm;

clock_gettime(CLOCK_REALTIME, &tm);

return tm.tv_nsec;

}

int main (int argc, char* argv[]) {

unsigned int dice_rolls = 12;

random::mt19937 rng(current_time_nanoseconds());

random::uniform_int_distribution<> six(1,6);

for(unsigned int i=0; i

cout << six(rng) << endl;

}

}

其中函数current_time_nanoseconds()给出当前时间(以纳秒为单位),用作种子。

这是一个更通用的类,用于获取范围内的随机整数和日期:

#include

#include

#include

#include

#include

#include "boost/date_time/posix_time/posix_time.hpp"

#include "boost/date_time/gregorian/gregorian.hpp"

using namespace std;

using namespace boost;

using namespace boost::posix_time;

using namespace boost::gregorian;

class Randomizer {

private:

static const bool debug_mode = false;

random::mt19937 rng_;

// The private constructor so that the user can not directly instantiate

Randomizer() {

if(debug_mode==true){

this->rng_ = random::mt19937();

}else{

this->rng_ = random::mt19937(current_time_nanoseconds());

}

};

int current_time_nanoseconds(){

struct timespec tm;

clock_gettime(CLOCK_REALTIME, &tm);

return tm.tv_nsec;

}

// C++ 03

// ========

// Dont forget to declare these two. You want to make sure they

// are unacceptable otherwise you may accidentally get copies of

// your singleton appearing.

Randomizer(Randomizer const&);     // Don't Implement

void operator=(Randomizer const&); // Don't implement

public:

static Randomizer& get_instance(){

// The only instance of the class is created at the first call get_instance ()

// and will be destroyed only when the program exits

static Randomizer instance;

return instance;

}

bool method() { return true; };

int rand(unsigned int floor, unsigned int ceil){

random::uniform_int_distribution<> rand_ = random::uniform_int_distribution<> (floor,ceil);

return (rand_(rng_));

}

// Is not considering the millisecons

time_duration rand_time_duration(){

boost::posix_time::time_duration floor(0, 0, 0, 0);

boost::posix_time::time_duration ceil(23, 59, 59, 0);

unsigned int rand_seconds = rand(floor.total_seconds(), ceil.total_seconds());

return seconds(rand_seconds);

}

date rand_date_from_epoch_to_now(){

date now = second_clock::local_time().date();

return rand_date_from_epoch_to_ceil(now);

}

date rand_date_from_epoch_to_ceil(date ceil_date){

date epoch = ptime(date(1970,1,1)).date();

return rand_date_in_interval(epoch, ceil_date);

}

date rand_date_in_interval(date floor_date, date ceil_date){

return rand_ptime_in_interval(ptime(floor_date), ptime(ceil_date)).date();

}

ptime rand_ptime_from_epoch_to_now(){

ptime now = second_clock::local_time();

return rand_ptime_from_epoch_to_ceil(now);

}

ptime rand_ptime_from_epoch_to_ceil(ptime ceil_date){

ptime epoch = ptime(date(1970,1,1));

return rand_ptime_in_interval(epoch, ceil_date);

}

ptime rand_ptime_in_interval(ptime floor_date, ptime ceil_date){

time_duration const diff = ceil_date - floor_date;

long long gap_seconds = diff.total_seconds();

long long step_seconds = Randomizer::get_instance().rand(0, gap_seconds);

return floor_date + seconds(step_seconds);

}

};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值