c庫偽隨機數(shù)發(fā)生器
成都創(chuàng)新互聯(lián)公司是專業(yè)的山海關(guān)網(wǎng)站建設(shè)公司,山海關(guān)接單;提供網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè),網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行山海關(guān)網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!rand
srand
大多時候用時間產(chǎn)生隨機發(fā)生器的seed
int GetRandomNum(int min, int max,int seed)
{
//srand((unsigned)time(NULL)); //生成seed
srand(seed);
return( rand() % (max - min) + min);
}
c++11 引入的偽隨機數(shù)發(fā)生器.隨機數(shù)抽象成隨機數(shù)引擎和分布兩部分.引擎用來產(chǎn)生隨機數(shù),分布產(chǎn)生特定分布的隨機數(shù)
常用的就是線性均勻分布
uniform_int_distribution
uniform_real_distribution
std::random_device rd;//來產(chǎn)生一個隨機數(shù)當(dāng)作種子
std::uniform_int_distribution<int> uni_dist(0, 9999999); //指定范圍的隨機數(shù)發(fā)生器
std::cout << uni_dist(rd) << std::endl;
還有一些其他發(fā)生器,如 伯努里分布、泊松分布、正態(tài)分布
// ConsoleApplication4.cpp : 定義控制臺應(yīng)用程序的入口點。
//
#include "stdafx.h"
#include <random>
#include <memory>
#include <iostream>
using namespace std;
class Random {
public:
const static unsigned int maxRand = std::random_device::max();
static Random& getInstance()
{
static Random instance;
return instance;
}
unsigned int getInteger() noexcept {
return (*dist)(rd);
}
unsigned int GetMTEngineInteger() noexcept {
return (*mtEngine)();
}
uint64_t GetMTEngine64Integer() noexcept {
return (*mtEngine64)();
}
unsigned int GetRand0Integer() noexcept {
return (*rand0Engine)();
}
auto GetRanlux48Integer() noexcept ->decltype(auto) {
return (*ranlux48Engine)();
}
private:
Random() noexcept {
mtEngine = std::make_shared<std::mt19937>(rd());
mtEngine64 = std::make_shared<std::mt19937_64>(rd());
dist = std::make_shared<std::uniform_int_distribution< unsigned int >>(std::uniform_int_distribution< unsigned int >(0, maxRand));
rand0Engine = make_shared<std::minstd_rand0>(rd());
ranlux48Engine = make_shared<std::ranlux48>(rd());
}
std::random_device rd;
std::shared_ptr<std::mt19937> mtEngine; //32-bit Mersenne Twister by Matsumoto and Nishimura, 1998
std::shared_ptr<std::mt19937_64> mtEngine64; //64-bit Mersenne Twister by Matsumoto and Nishimura, 2000(馬特賽特旋轉(zhuǎn)演算法)
std::shared_ptr<std::minstd_rand0> rand0Engine;
std::shared_ptr<std::ranlux48> ranlux48Engine;
std::shared_ptr<std::uniform_int_distribution< unsigned int > > dist;
};
int main()
{
cout << Random::getInstance().GetMTEngineInteger() << endl;
cout << Random::getInstance().GetMTEngine64Integer() << endl;
cout << Random::getInstance().GetRand0Integer() << endl;
cout << Random::getInstance().GetRanlux48Integer() << endl;
cout << Random::getInstance().getInteger() << endl;
return 0;
}
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
本文名稱:c++產(chǎn)生隨機數(shù)的方法-創(chuàng)新互聯(lián)
路徑分享:http://aaarwkj.com/article26/ccoijg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、網(wǎng)站排名、品牌網(wǎng)站制作、網(wǎng)站導(dǎo)航、標(biāo)簽優(yōu)化、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容