6

std::mt19937 gen(2007)我在 C++ 和RandStream.create('mt19937ar','seed',2007)Matlab 中尝试过。我也尝试过不同的结构,但我找不到特定的种子结构来在 Matlab 和 c++ 之间找到相同的随机数。我该如何处理?

4

2 回答 2

3

您希望随机生成器具有确定性,并在两个不同的实现中以相同的方式工作。

不能保证 Matlab 和 c++::std 实现会产生相同的结果。尽管认为他们应该这样做是合理的——毕竟这是相同的算法。根据Wikipedia的说法,这些实现有很多不同之处。最值得注意的是产生不同结果的 32 位和 64 位实现之间的差异。

为了克服这个障碍,在一个工具中生成数字,然后在另一个工具中使用相同的序列。或者使用您自己的算法 -这里有一些想法。

于 2014-07-23T11:25:54.520 回答
1

The difference (most likely) stems from the use of uniformly-distributed pseudo-random numbers in C++, while MATLAB code uses normally-distributed pseudo-random numbers. Try rand/randi instead randn in the MATLAB code (i.e. unformly-distributed integers instead of normally distributed doubles).

More on MATLAB side of the story: http://www.mathworks.com/help/matlab/random-number-generation.html

于 2014-07-23T11:36:08.060 回答