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

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

  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

有关编译器优化的更完整信息,请参阅优化通知
选择粘滞按钮颜色: 
Orange (only for download buttons)