OpticStudio Random Number Generator Seed

  • 17 August 2022
  • 8 replies


Is there a way to set the seed for the random number generator so that every time I run a Ray Trace analysis I get the exact same results?



Using OpticStudio 22.1 Professional



Best answer by Mark.Nicholson 19 August 2022, 00:54

View original

8 replies

Userlevel 7
Badge +3

Use Sobol Sampling. That will give a ‘uniformly random’ rays that is used every time you trace rays

Note when you optimize, OS uses the same random number generator, so if you evaluate the merit function twice without making changes you get the same results. You don’t need to use Sobel in this case, although it is still useful.


Thank you!


Apparently Sobol sampling makes the generation of rays from a source repeatable.


However--as I understand it at this time--the random number generator seed for other processes like scattering cannot be fixed, and thus the results of the overall model will vary from run-to-run. 

Userlevel 6
Badge +2

Scattering is by nature of existing models always random, so there is no way to pre-determine where the scattered ray will go.  Scattering only makes sense when a statistically significant number of rays are traced, so if you’re getting wildly different results from run-to-run when you have scattering turned on, then you don’t have enough rays tracing through your system.

If you want to keep the scattered rays consistent between different analysis, you should either save a ZRD file (all non-sequential analysis can read a ZRD file) or convert a ZRD file to a SDF source file (What is the easiest way to convert ZRD file to SDF file? | Zemax Community).

Userlevel 4
Badge +1

Mark and MichaelH have provided good and valid answers. The Sobol sampling is useful for creating a repeating but still ‘random’ selection of rays. For scattering, the usual strategy is to have lots of rays. However, if you are in need of a scatter that is random spatially but consistent over time, you’ll probably want to look into using a DLL for scatter. There is a help page addressing this topic, titled ‘Random Number Generation in user-Defined DLLs for NSC’, though it focuses mainly on the importance of 32-bit vs 16-bit generators.


Thanks all for the responses. 


I appreciate statistical significance and probability in the real world, but I live in the computer.  So for creating and testing a very specific model, I want to verify some very specific things and results… consistently.  After I’m satisfied, I plan to run the model with a sufficiently large number of rays for final analysis.


What I really want is Zemax to expose their random number generator to the end user so I can fix the starting seed, continue to use all of their built-in scattering functions and any other follow-on pseudorandom processes, and get consistent results run after run.  I’ve reached out to Zemax directly on this and--at this time--I can’t manually seed the random number generator.  


Writing my own DLL seems like the best workaround at this time.  Thanks again!


+1 on the request from jp774.  I’ve got a bulk scattering model and if I need to prove to a customer that two different versions of Zemax give the same result, it seems like the only way check this is to trace N rays, where N approaches infinity.   (made worse because the number of rays reaching the detector is very small to begin with).

Regards,  John                                          dated: Jan 2023.

Userlevel 7
Badge +3

+1 for exposing the NSC ray tracing seed in the GUI.  It should allow the user to obtain repeatable scattering via the standard Ray Trace Control tool as well as on Layouts. 

If you are willing to use the ZOS-API, then it is currently possible to set the random seed generator so that repeated ray traces will yield the same scattering results. 


I typically do this using the Interactive mode.  Here’s a Matlab script that illustrates how it works:


However, this requires all ray traces to be instigated from within ZOS-API.  Having the same capability in the GUI would be helpful.




Jeff -                                                date: 1/8/2023

Thanks for the tip.   That worked.  Something is off with the GUI method - any non-zero value for the Random seed causes one, and only one, of the detectors to never see any rays.  Oh well. 

Here’s the rest of the method to pull the info after Jeff’s note, if anyone wants it.  Found this in the ZOS-api help with an example.