I have legacy C++ code that I wrote to generate uniform random numbers and a Gaussian distribution. It implements algorithms by Dr. George Marsaglia that are extremely fast. (I was using them to generate skazillions of samples for Monte Carlo high-dimensional integration.)
我编写了遗留的c++代码来生成统一的随机数和高斯分布。它实现了George Marsaglia博士的算法,速度非常快。(我用它们来为蒙特卡洛高维集成制造样本。)
I think it would be a good idea to re-factor the generator and distribution to work with the new C++11 std::random scheme.
我认为将生成器和分布重构与新的c++ 11 std::随机方案是一个好主意。
Can anyone point me to a tutorial or a good reference for std::random that includes the necessary info for how to extend it? Example code would be ideal.
有谁能给我指出一个教程或一个关于std::random的很好的参考资料,包括如何扩展它的必要信息?示例代码将是理想的。
UPDATE. Thanks for everyone's help. I have now written a drop-in replacement for the std::normal_distribution that ships with Visual C++ 2010. On my machine, the replacement is 26% faster when fed by the default engine. I am a little disappointed that the difference is not bigger, but hey, that's my problem. :-)
更新。谢谢每个人的帮助。我现在已经为std::normal_distribution(使用Visual c++ 2010发布)编写了一个临时替代。在我的机器上,当使用默认引擎时,替换的速度要快26%。我有点失望,差距不是很大,但嘿,这是我的问题。:-)
2 个解决方案
#1
5
N3376 is the latest draft C++ standard (this is post C++11, but is an excellent snapshot of C++11).
N3376是最新的c++标准草案(这是发布的c++ 11,但它是c++ 11的优秀快照)。
Everything C++11-random is in: 26.5 Random number generation [rand]
所有C+ 11-random都在:26.5随机数生成[rand]
26.5.1.4 Random number engine requirements [rand.req.eng] has all of the requirements your uniform random number generator would need to fulfill.
26.5.1.4随机数引擎要求所有的要求你的统一随机数发生器需要完成。
26.5.1.6 Random number distribution requirements [rand.req.dist] has all of the requirements your Gaussian distribution would need to fulfill.
26.5.1.6随机数分布要求[rand.req。满足高斯分布所需要的所有要求。
26.5.8.5.1 Class template normal_distribution [rand.dist.norm.normal] is the section describing the std-defined Gaussian distribution.
26.5.8.5.1类模板normal_distribution [rand.dist.norm。是描述std定义的高斯分布的部分。
The C++11 <random>
is very STL-like in that it sets up requirements for random number generators (containers), and random distributions (algorithms), and then the client can mix and match the two. It's a really very cool design.
c++ 11
Sorry, I don't know of a good tutorial. The C++ standard is an excellent reference and a lousy tutorial. However you are obviously well educated in the domain of random numbers. So assuming you know a thing or two about C++, the C++ standard may not be too bad.
对不起,我不知道有什么好的教程。c++标准是一个优秀的参考和糟糕的教程。但是你显然在随机数领域受过良好的教育。因此,假设您对c++有所了解,那么c++标准可能不会太糟糕。
Open source implementations of <random>
are available if you want to peruse their source (for an example). One example is libc++. All they ask is that you retain their copyright notices if you reuse any of their code.
如果您想仔细阅读它们的源代码(例如),可以使用
Edit
编辑
You are uniquely qualified to write this tutorial. :-)
您有资格编写本教程。:-)
#2
1
You can learn a lot by reading boost library sources, since many proposals in C++11 were adopted from boost.
通过阅读boost库资源,您可以学到很多东西,因为许多c++ 11中的建议都是从boost中采用的。
Take a look at the interface of an example rng engine here:
看看这里的示例rng引擎的接口:
http://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine
http://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine
I would start by implementing the min max seed and operator() functionalities to see if it passes as a valid engine for C++11
我将首先实现最小最大种子和运算符()功能,看看它是否作为c++ 11的有效引擎传递
#1
5
N3376 is the latest draft C++ standard (this is post C++11, but is an excellent snapshot of C++11).
N3376是最新的c++标准草案(这是发布的c++ 11,但它是c++ 11的优秀快照)。
Everything C++11-random is in: 26.5 Random number generation [rand]
所有C+ 11-random都在:26.5随机数生成[rand]
26.5.1.4 Random number engine requirements [rand.req.eng] has all of the requirements your uniform random number generator would need to fulfill.
26.5.1.4随机数引擎要求所有的要求你的统一随机数发生器需要完成。
26.5.1.6 Random number distribution requirements [rand.req.dist] has all of the requirements your Gaussian distribution would need to fulfill.
26.5.1.6随机数分布要求[rand.req。满足高斯分布所需要的所有要求。
26.5.8.5.1 Class template normal_distribution [rand.dist.norm.normal] is the section describing the std-defined Gaussian distribution.
26.5.8.5.1类模板normal_distribution [rand.dist.norm。是描述std定义的高斯分布的部分。
The C++11 <random>
is very STL-like in that it sets up requirements for random number generators (containers), and random distributions (algorithms), and then the client can mix and match the two. It's a really very cool design.
c++ 11
Sorry, I don't know of a good tutorial. The C++ standard is an excellent reference and a lousy tutorial. However you are obviously well educated in the domain of random numbers. So assuming you know a thing or two about C++, the C++ standard may not be too bad.
对不起,我不知道有什么好的教程。c++标准是一个优秀的参考和糟糕的教程。但是你显然在随机数领域受过良好的教育。因此,假设您对c++有所了解,那么c++标准可能不会太糟糕。
Open source implementations of <random>
are available if you want to peruse their source (for an example). One example is libc++. All they ask is that you retain their copyright notices if you reuse any of their code.
如果您想仔细阅读它们的源代码(例如),可以使用
Edit
编辑
You are uniquely qualified to write this tutorial. :-)
您有资格编写本教程。:-)
#2
1
You can learn a lot by reading boost library sources, since many proposals in C++11 were adopted from boost.
通过阅读boost库资源,您可以学到很多东西,因为许多c++ 11中的建议都是从boost中采用的。
Take a look at the interface of an example rng engine here:
看看这里的示例rng引擎的接口:
http://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine
http://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine
I would start by implementing the min max seed and operator() functionalities to see if it passes as a valid engine for C++11
我将首先实现最小最大种子和运算符()功能,看看它是否作为c++ 11的有效引擎传递