[C++11]C++11帶來的隨機數產生器

來源:互聯網
上載者:User

C++11帶來諸多特性,random就是其一.

1. random_device

  標準庫提供了一個非確定性隨機數產生裝置.在Linux的實現中,是讀取/dev/urandom裝置;Windows的實現居然是用rand_s,在這裡強烈譴責一下.

  random_device提供()操作符,用來返回一個min()到max()之間的一個數字.如果是Linux(Unix Like或者Unix)下,都可以使用這個來產生高品質的隨機數,可以理解為真隨機數.

#include <iostream>#include <random>int main(){  std::random_device rd;  for(int n=0; n<20000; ++n)    std::cout << rd() << std::endl;  return 0; }

2. random number engine

  標準把隨機數抽象成隨機數引擎和分布兩部分.引擎用來產生隨機數,分布產生特定分布的隨機數(比如平均分布,正太分布等).

  標準提供三種常用的引擎:linear_congruential_engine,mersenne_twister_engine和subtract_with_carry_engine.第一種是線性同餘演算法,第二種是梅森旋轉演算法,第三種帶進位的線性同餘演算法.第一種是最常用的,而且速度也是非常快的; 第二種號稱是最好的偽隨機數產生器;第三種沒用過....

  隨機數引擎接受一個整形參數當作種子,不提供的話,會使用預設值. 推薦使用random_device來產生一個隨機數當作種子.(windows下愛咋整咋整,誰叫windows的random_device是調用rand_s)

#include <iostream>#include <random>int main(){  std::random_device rd;  std::mt19937 mt(rd());  for(int n = 0; n < 10; n++)    std::cout << mt() << std::endl;  return 0;}

3. random number distributions

  標準提供各種各樣的分布,不過我們經常用的比較少,比如平均分布,正太分布...使用也很簡單

//平均分布#include <random>#include <iostream>int main(){    std::random_device rd;    std::mt19937 gen(rd());    std::uniform_int_distribution<> dis(1, 6);    for(int n=0; n<10; ++n)        std::cout << dis(gen) << ' ';    std::cout << '\n';}
//正太分布#include <iostream>#include <iomanip>#include <string>#include <map>#include <random>#include <cmath>int main(){    std::random_device rd;    std::mt19937 gen(rd());     // values near the mean are the most likely    // standard deviation affects the dispersion of generated values from the mean    std::normal_distribution<> d(5,2);     std::map<int, int> hist;    for(int n=0; n<10000; ++n) {        ++hist[std::round(d(gen))];    }    for(auto p : hist) {        std::cout << std::fixed << std::setprecision(1) << std::setw(2)                  << p.first << ' ' << std::string(p.second/200, '*') << '\n';    }}

 

參考:

1. http://en.cppreference.com/w/cpp/numeric/random

2. windows下的高品質隨機數產生器,參考CryptGenRandom API, 以及http://www.cnblogs.com/egmkang/archive/2011/03/05/1971586.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.