A typical algorithm for VSL random number generators is as follows:
Create and initialize stream/streams. Functions vslNewStream, vslNewStreamEx, vslCopyStream, vslCopyStreamState, vslLeapfrogStream, vslSkipAheadStream.
Call one or more RNGs.
Process the output.
Delete the stream/streams. Function vslDeleteStream.
You may reiterate steps 2-3. Random number streams may be generated for different threads.
The following C 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.
#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;
}
The Fortran version of the same example is below:
include 'mkl_vsl.f90'
program MKL_VSL_GAUSSIAN
USE MKL_VSL_TYPE
USE MKL_VSL
real(kind=8) r(1000) ! buffer for random numbers
real(kind=8) s ! average
real(kind=8) a, sigma ! parameters of normal distribution
TYPE (VSL_STREAM_STATE) :: stream
integer(kind=4) errcode
integer(kind=4) i,j
integer brng,method,seed,n
n = 1000
s = 0.0
a = 5.0
sigma = 2.0
brng=VSL_BRNG_MT19937
method=VSL_RNG_METHOD_GAUSSIAN_ICDF
seed=777
! ***** Initializing *****
errcode=vslnewstream( stream, brng, seed )
! ***** Generating *****
do i = 1,10
errcode=vdrnggaussian( method, stream, n, r, a, sigma )
do j = 1, 1000
s = s + r(j)
end do
end do
s = s / 10000.0
! ***** Deinitialize *****
errcode=vsldeletestream( stream )
! ***** Printing results *****
print *,"Sample mean of normal distribution = ", s
end
Additionally, examples that demonstrate usage of VSL random number generators are available in the following directories:
${MKL}/examples/vslc/source
${MKL}/examples/vslf/source
Copyright © 1994 - 2011, Intel Corporation. All rights reserved.