Developer Reference for Intel® oneAPI Math Kernel Library for C

ID 766684
Date 11/07/2023
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

VS RNG Usage ModelIntel® oneMKL RNG Usage Model

A typical algorithm for VSoneMKL random number generators is as follows:

  1. Create and initialize stream/streams. Functions vslNewStream, vslNewStreamEx, vslCopyStream, vslCopyStreamState, vslLeapfrogStream, vslSkipAheadStream, vslSkipAheadStreamEx.

  2. Call one or more RNGs.

  3. Process the output.

  4. Delete the stream or streams with the function vslDeleteStream.

NOTE:

You may reiterate steps 2-3. Random number streams may be generated for different threads.

The following example demonstrates generation of a random stream that is output of basic generator MT19937. The seed is equal to 777. The stream is used to generate 10,000 normally distributed random numbers in blocks of 1,000 random numbers with parameters a = 5 and sigma = 2. Delete the streams after completing the generation. The purpose of the example is to calculate the sample mean for normal distribution with the given parameters.

Example of VS RNG Usage

#include <stdio.h>
#include "mkl_vsl.h"
 
int main()
{
   double r[1000]; /* buffer for random numbers */
   double s; /* average */
   VSLStreamStatePtr stream;
   int i, j;
    
   /* Initializing */        
   s = 0.0;
   vslNewStream( &stream, VSL_BRNG_MT19937, 777 );
    
   /* Generating */        
   for ( i=0; i<10; i++ ) {
      vdRngGaussian( VSL_RNG_METHOD_GAUSSIAN_ICDF, stream, 1000, r, 5.0, 2.0 );
      for ( j=0; j<1000; j++ ) {
         s += r[j];
      }
   }
   s /= 10000.0;
    
   /* Deleting the stream */        
   vslDeleteStream( &stream );
    
   /* Printing results */        
   printf( "Sample mean of normal distribution = %f\n", s );
    
   return 0;
}

Additionally, examples that demonstrate usage of VS random number generators are available in:

${MKL}/examples/vslc/source