yohhoyの日記

技術的メモをしていきたい日記

標準ライブラリrandomの分布生成器は処理系依存

C++標準ライブラリ<random>で提供される分布生成器(distribution)は、具体的なアルゴリズムまで規定しないため処理系毎に異なる乱数列が得られる。乱数生成エンジン(engine)のアルゴリズム/パラメータが言語仕様で定義されるのとは対照的(→id:yohhoy:20130719)。

#include <random>
std::mt19937 rng;                 // engine
std::normal_distribution<> dist;  // distribution

// rng()で生成される値は全処理系で同一
// dist(rng)で生成される値は処理系依存

複数の処理系間で同一乱数列を得る必要があるときは、Boost.Randomライブラリなどを利用すること。正直、C++標準でポータブルな分布生成器まで提供した方が良かったのでは…

2016-02-25追記:Boost.Randomライブラリの場合も、バージョン間で分布生成器アルゴリズムが変更される可能性があるため注意。詳細はコメント参照のこと。(thanks to id:faith_and_brave さん)

C++11 26.5.8.1/p3より引用。

The algorithms for producing each of the specified distributions are implementation-defined.

N1398 A Proposal to Add an Extensible Random Number Facility to the Standard Libraryより根拠箇所を引用(下線部は強調)。

C. Separation of Engines and Distributions
(snip)

This proposal attempts to specify the engines precisely; two different implementations, with the same seed, should return the same output sequence. This forces implementations to use the well-researched engines specified hereinafter, and users can have confidence in their quality and the limits thereof.

On the other hand, the specifications for the distributions only define the statistical result, not the precise algorithm to use. This is different from engines, because for distribution algorithms, rigorous proofs of their correctness are available, usually under the precondition that the input random numbers are (truely) uniformly distributed. For example, there are at least a handful of algorithms known to produce normally distributed random numbers from uniformly distributed ones. Which one of these is most efficient depends on at least the relative execution speeds for various transcendental functions, cache and branch prediction behaviour of the CPU, and desired memory use. This proposal therefore leaves the choice of the algorithm to the implementation. It follows that output sequences for the distributions will not be identical across implementations. It is expected that implementations will carefully choose the algorithms for distributions up front, since it is certainly surprising to customers if some distribution produces different numbers from one implementation version to the next.

(snip)

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1398.html

関連URL