random為隨機數庫的頭文件,提供產生隨機數與偽隨機數的類。

生成器與分布概述 編輯

  • 均勻隨機比特生成器(Uniform random bit generator,URBG),產生均勻分布的整數的序列:包括:
    • 隨機數引擎(random number engine):產生偽隨機的均勻分布的整數的序列
      • linear_congruential_engine 類模板: 線性同餘算法
      • mersenne_twister_engine 類模板: 梅森旋轉算法
      • subtract_with_carry_engine 類模板:帶進位減法的滯後斐波那契隨機數算法
    • 真隨機數引擎,如果有的話
  • 隨機數分布(Distributions),如均勻分布、正態分布、泊松分布,即把均勻分布的隨機數轉為不同的統計分布。
  • 隨機數引擎適配器使用基礎隨機數引擎產生偽隨機數,通常用於改變譜特性:
    • discard_block_engine 類模板:拋棄隨機數引擎的一些輸出
    • independent_bits_engine 類模板:把隨機數引擎的輸出包裝為特定比特數的塊
    • shuffle_order_engine 類模板:把隨機數引擎的輸出按照不同的序遞交

示例:

std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(1,6);
int dice_roll = distribution(generator);  // generates number in the range 1..6

//Code copied from https://codereview.stackexchange.com/questions/109260/seed-stdmt19937-from-stdrandom-device
std::vector<uint32_t> random_data(624);
std::random_device source;
std::generate(random_data.begin(), random_data.end(), std::ref(source));
std::seed_seq seeds(random_data.begin(), random_data.end());
std::mt19937 engine(seeds);
//Or:
//std::mt19937_64 engine(seeds);

using mt_engine = std::mersenne_twister_engine</*...*/>;
constexpr size_t state_size = mt_engine::state_size * mt_engine::word_size / 32;
std::vector<uint32_t> random_data(state_size);

預定義的隨機數引擎 編輯

  • minstd_rand0: std::linear_congruential_engine<std::uint_fast32_t, 16807, 0, 2147483647>。1969由Lewis, Goodman與Miller提出,1988年被Park與Miller稱作"Minimal standard"
  • minstd_rand: std::linear_congruential_engine<std::uint_fast32_t, 48271, 0, 2147483647>。更新的"Minimum standard"。1993年由Park, Miller與Stockmeyer提出。
  • mt19937:std::mersenne_twister_engine<std::uint_fast32_t, 32, 624, 397, 31,0x9908b0df, 11,0xffffffff, 7,0x9d2c5680, 15,0xefc60000, 18, 1812433253>。32比特梅森旋轉法,1998年由Matsumoto與Nishimura
  • mt19937_64:std::mersenne_twister_engine<std::uint_fast64_t, 64, 312, 156, 31,0xb5026f5aa96619e9, 29,0x5555555555555555, 17,0x71d67fffeda60000, 37,0xfff7eee000000000, 43, 6364136223846793005>。2000年由Matsumoto與Nishimura提出的64比特梅森旋轉法。
  • ranlux24_base:std::subtract_with_carry_engine<std::uint_fast32_t, 24, 10, 24>
  • ranlux48_base:std::subtract_with_carry_engine<std::uint_fast64_t, 48, 5, 12>
  • ranlux24:std::discard_block_engine<std::ranlux24_base, 223, 23>。24比特RANLUX生成器,1994年由Martin Lüscher與Fred James
  • ranlux48:std::discard_block_engine<std::ranlux48_base, 389, 11>。48比特RANLUX生成器,1994年由Martin Lüscher與Fred James
  • knuth_b:std::shuffle_order_engine<std::minstd_rand0, 256>。
  • default_random_engine:由實現定義。可能是std::mt19937的別名。
  • std::random_device:由硬件熵源。但如果沒有真隨機數發生器,也可以用偽隨機數代替。Visual C++編譯器保證產生密碼級安全的偽隨機數。

隨機數分布 編輯

  • 均勻分布
    • uniform_int_distribution類模板
    • uniform_real_distribution類模板
  • 伯努利分布
    • bernoulli_distribution類
    • binomial_distribution類模板
    • negative_binomial_distribution類模板
    • geometric_distribution類模板
  • 泊松分布
    • poisson_distribution類模板
    • exponential_distribution類模板
    • gamma_distribution類模板
    • weibull_distribution類模板
    • extreme_value_distribution類模板
  • 正態分布
    • normal_distribution類模板
    • lognormal_distribution類模板
    • chi_squared_distribution類模板
    • cauchy_distribution類模板
    • fisher_f_distribution類模板
    • student_t_distribution類模板
  • 採樣分布
    • discrete_distribution類模板
    • piecewise_constant_distribution類模板
    • piecewise_linear_distribution類模板
  • 工具
    • generate_canonical函數模板:在[0, 1)中給定精度均勻分布
    • seed_seq類:通用無偏種子序列生成器。消耗構造時傳入的一個序列的整數值,產生32比特均勻分布的一個整數值。用於給多個隨機數引擎作種子或者一個隨機數引擎需要多個種子。

C語言隨機庫 編輯

不建議使用,定義在頭文件<cstdlib>

  • rand函數:產生偽隨機數
  • srand函數:設置隨機數種子
  • RAND_MAX宏常量:std::rand生成的最大可能值