Random Number Generation in user-Defined DLLs for NSC

Many of the DLLs that are included with the OpticStudio installation for use in non-sequential mode require random numbers to be generated inside of the DLL (generally to allow for Monte Carlo sampling of a distribution). In all of the DLLs for which random numbers need to be generated (the full list is provided below), OpticStudio uses the Mersenne Twister algorithm to produce these numbers (see http://en.wikipedia.org/wiki/Mersenne_Twister for a description of the algorithm). The implementation used in OpticStudio is based on the standard MT19937 algorithm, which uses a 32-bit word length.

The list of DLLS included with the OpticStudio installation that use the Mersenne Twister algorithm are:

Bulk Scattering

  • bulk_samp_1.dll
  • Mie.dll
  • Phosphor.dll
  • poly_bulk_scat.dll
  • Rayleigh.dll

Diffractive

  • diff_samp_1.dll

Source File

  • fiber1.dll

Surface Scattering

  • Gaussian_XY.dll
  • K-correlation.dll
  • Lambertian.dll
  • TwoGaussian.dll

We strongly recommend that anyone creating their own DLL for use in non-sequential mode which requires the use of random numbers simply adopt our implementation of the Mersenne Twister algorithm, or write their own 32-bit random number generator, rather than using the standard Windows API function rand. That is because the rand function is 16-bit, meaning that it can only be used to generate 2^16 = 65,536 unique random numbers. Thus, in NSC – when it is common to trace millions of rays – the use of the rand function can result in the generation of many repeated numbers, leading to "clumping" of data.

Next: