Replacing the Default Random Number Generator with a Multiple Stream Generator

From Support

Jump to: navigation, search

As better, faster, or more flexible pseudo-random number generation codes are developed, they can be easily included in your SIGMA generated simulation engines. This section gives a detailed example for doing this. In this example, we will assume that you wish to use 6 different random number streams in your simulation model. See Inputs/Outputs for some reasons why you might be interested in using multiple streams in a simulation.

You first need to download the random number generator software you wish to use and add it to your C workspace. To illustrate, we will use RngStream.c and RngStream.h by Pierre L’Ecuyer available here. Both these files will need to be added to your C workspace.

Make four simple changes to your SIGMA-generated C code

1. Add the following line after #include "sigmalib.h"

#include "RngStream.h" 

2. Define i and seed[ ] in initialize(int argc, const char** argv) by adding the following two lines after char y_n[2] = 'p';/* yes/no for file overwrite*/

int i;
unsigned long seed[6];

3. Initialize functions in RngStream.c (also in initialize( ))

for (i=0;i<6;i++)

for (i=0;i<numstreams; i++){
        //Create stream i
        stream[i] = RngStream_CreateStream("");
        //RngStream_SetPackageSeed (unsigned long seed[6]);

        //Reinitialize stream i to the beginning of its next substream

        //Can generate antithetic variates (1=1-U, 0=U)
        //rngstream_setantithetic(stream[i], 0);

        //Increased precision (0=32 bit, 1=53 bits)

4. In the /* EVENT FUNCTIONS */ Replace RND with RngStream_RandU01(stream[1]) (or other functions for as antithetic streams, etc.) everywhere in the event codes. For example, if Sigma generates the following line of code

event_time = current_time + 3+5*RND;

replace it with

event_time = current_time + 3+5*RngStream_RandU01(stream[1]);

Finally, add the following code to the bottom of sigmalib.h, where numstreams is the number of different streams you wish to use in your simulation.

//Multiple random number streams
#include "RngStream.h"
#define numstreams 6
RngStream stream[numstreams];

Now you compile and link these programs to create your simulation engine as before. If you use the compiling template provided for Microsoft Visual C/C++, just double-click on mysigmasimulation.dsw and press F7 to create an executable program of your SIGMA simulation engine. Your simulation engine will again be called MySigmasimulation.exe and is in the debug subfolder. You can now rename it anything you wish and include in a spreadsheet or web site – or double click it to run it.

Back to Technical References

Personal tools