产生随机数的叫随机数生发器
生成随机数
const unsigned zseed=time(0);
void solve()
{
//随机数生发器
mt19937_64 m{zseed};
//种子
rep(i,1,5)
cout<<m()<<endl;
return;
}
重排序列
const unsigned zseed=time(0);
mt19937_64 zgen{zseed};
void solve()
{
vector<int>a={1,2,3,4,5,6};
shuffle(begin(a),end(a),zgen);
for(auto i:a) cout<<i<<' ';
cout<<endl;
return;
}
产生范围内均匀分布的整数随机数
const unsigned zseed=time(0);
mt19937_64 zgen{zseed};
struct UI{
//随机数的分布器
//产生随机数的区间是[a,b]
uniform_int_distribution<int>u;
//随机数的生发器
mt19937_64& gen{zgen};
int get()
{
return u(gen);
}
UI(int a=0,int b=1):u(a,b){};
};
void solve()
{
UI u{-2,2};
rep(i,1,5) cout<<u.get()<<endl;
return;
}
产生范围内均匀分布的小数
左闭右开$ [ ) $
const unsigned zseed=time(0);
mt19937_64 zgen{zseed};
struct UD{
uniform_real_distribution<db>u;
mt19937_64& gen{zgen};
db get()
{
return u(gen);
}
UD(db a=0,db b=1) :u{a,b}{}
};
标签:mt19937,int,c++,生成,zgen,64,随机数,zseed
From: https://www.cnblogs.com/cxy8/p/18007745