简体   繁体   English

使用32位Mersenne Twister生成64位值

[英]Generating 64-bit values with a 32-bit Mersenne Twister

According to this Boost documentation page , the 64-bit variant of the Mersenne Twister is much slower than its 32-bit counterpart (which makes sense). 根据Boost文档页面 ,Mersenne Twister的64位版本比其32位版本慢得多(这很有意义)。 As I understand, a lot of the features introduced in C++11, including random number generation, are basically Boost in the standard library. 据我了解,C ++ 11中引入的许多功能(包括随机数生成)基本上都是标准库中的Boost。 This leads me to believe that 32-bit MT performance in standard C++ is also better. 这使我相信标准C ++中的32位MT性能也更好。

I'm writing a raytracer (for fun, mostly), and speed is one of my primary concerns. 我正在编写raytracer(主要是出于娱乐目的),而速度是我主要关心的问题之一。 Essentially all the numerical values are represented as double precision floats. 本质上,所有数值都表示为双精度浮点数。 My question is, since the 32-bit MT is considerably faster, can I use it to generate doubles? 我的问题是,由于32位MT的速度要快得多,我可以用它来生成双精度吗? What drawbacks (precision loss, performance, etc.) can I expect? 我可以期待哪些缺点(精度损失,性能等)?

For this I am adding one assumption that you did not mention: I am assuming you are doing one random draw per double. 为此,我添加了一个您没有提到的假设:我假设您每双进行一次随机抽奖。 Obviously you can get twice the randomness by doing two draws. 显然,您可以通过两次平局获得两倍的随机性。

The first question is really "does 32-bits of pseudorandomness have enough randomness for my ray tracer." 第一个问题确实是“ 32位伪随机性对于我的光线跟踪器是否具有足够的随机性”。 My guess is yes. 我的猜测是。 Most raytracers are only shooting out a few million rays, so you wont notice that there's only 4 billion bits of pseudorandomness. 大多数光线追踪器仅发出几百万条光线,因此您不会注意到只有40亿比特的伪随机性。

Second question is "can I distribute the pseudorandomness across the domain of double values I care about." 第二个问题是“我是否可以在我关心的双精度值域内分配伪随机性”。 Again, my guess is yes. 同样,我的猜测是肯定的。 If you are shooting rays in a 90 degree field, and there are 4 billion possible results from one pseudorandom draw. 如果您在90度视野中拍摄光线,则一次伪随机抽签可能会产生40亿个结果。 For perspective, a sniper looking through a high power scope sees millions of times less angular precision than the average difference between those pseudorandom vectors. 从透视图上看,狙击手在高倍镜下观察到的角精度比这些伪随机向量之间的平均差小数百万倍。

All that being said, profile your code. 这么说,对您的代码进行概要分析。 I'd give a 99.9998% chance that your raytracing code itself takes much longer than the pseudorandom generation unless your scenes all consist of single non-reflective spheres 除非您的场景全部由单个非反射球组成,否则我有99.9998%的机会使您的光线跟踪代码本身花费时间比伪随机生成的时间长得多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM