Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.
7032 Discussions

## Random Number Stream: automatic change of the seed

Beginner
857 Views

I have to generate a sequence of random number (with uniform distribution).
So i can use the vslnewstream function wherein i have to specify the seed number and the other parameters.
Furthermore I need to use this function within a cicle, so i would like to change seed each time i call the function.
In the IMSL library this can be made by using a seed number=-1. In this way the seed is changed automatically each time the function generating the number is called.
Is there a way to do this by using the MKL ruotines?

Thank you

Clodxp

6 Replies
Employee
857 Views

Hello,

Can I know the reasons for which you do not want to use the same random stream (initialized with the same seed) for the generation of the next portion of uniform random numbers and need to change the seed?

Anyway, below is one possible approach to change the seed after each call to the uniform generator:

#define N 1000
#define M 100

int main()
{
int i;
unsigned int seed_array;
double r;
double a, b;
VSLStreamStatePtr stream;

/* initialize array of seeds */

for( i = 0; i < N; i++ )
{
seed_array = ;
}

for( i = 0; i < N; i++ )
{
errcode = vslNewStream( &stream, VSL_BRNG_MT19937, seed_array );
errcode = vdRngUniform( METHOD, stream, M, r, a, b );

/* Process array r */

errcode = vslDeleteStream( &stream );
}

}

The scheme above admits some modifications.

Depending on the requirements of your application block-splitting methods and the basic random generators which support independent random streams like MT2203 and Wichmann-Hill could probably be helpful.

Andrey

Beginner
857 Views
Hi Andrey,
I need to use the random generator within an iterative cycle: in particular i need to create a new, uniform generated, vector for each iteration. So i need a new seed, for each iteration.
From your email i understand that, maybe, it can be obtained in a different way. If it is so, please explain it to me.
Regarding the question, i'm asking if exist a flag, or a control parameter, that changes automatically the seed(without change it manually within the code), like in the IMSL routines. As i understand from your answer this can be obtained only by manually changing the seed.
Thank you
Clodxp
Employee
857 Views

Hello Clodxp,

Intel MKL random number generators do not support automatic changing the seed via flags (or control parameters). Instead the library gives opportunity to explicitly initialize the random streams and, thus, to obtain the random numbers according to your requirements.

Manual initialization of the stream with a new seed on each iteration i could be done as described in my previous post:

1. Create the random stream with a new seed

2. Generate the array of uniform random numbers

3. Remove the random stream

You also can create N random streams in advance using N different seeds and obtain the random numbers from each of those streams.

The reasons for which you need to reset the seed on each iteration of the loop are still not clear.
Can you please clarify them is possible?
Do you expect that M random numbers generated on iteration i would be dependent on M random numbers on iteration i+1?

#define N 1000
#define M 100

int main()
{
int i;
double r;
double a, b;
VSLStreamStatePtr stream;

/* initialize */
errcode = vslNewStream( &stream, VSL_BRNG_MT19937, seed );
for( i = 0; i < N; i++ )
{
/* Generate M uniform numbers */
errcode = vdRngUniform( METHOD, stream, M, r, a, b );
/* Process array r */

}

errcode = vslDeleteStream( &stream );
...
}

Thanks,
Andrey

Beginner
857 Views
Hi Andrey
The solution you propose is ok, and works fine.
The only "issue" is that each time i launch the code i always have the same stream of random numbers for each iteration, even if i change the seed like you suggested, since the seed changes in a deterministic way.
Am i wrong?
Please tell me if i've clarified the problem.

Thanks
Clodxp
Employee
857 Views

Hello Clodxp,

Intel MKL random number generators are based on the deterministic algorithms. So, we should expect repeatability of the generator output from run to run given the generator is initialized with the same seed. This is also the case if on each loop iteration the generator is initialized with another seed which remains the same from run to run of the whole application.

This feature of the deterministic generator can be important in some cases. In particular, when you tune your stochastic model you might want to have the same output in the simulations from run to run. In other applications like cryptograpical absence of the output reproducibility is important.

The fast way to have a new seed in your application is to get it by means of the system tools, e.g., time counter like __rdtsc() which returns the CPU time stamp.
However, using this (or another) approach for seed generation should be considered from perspective of requirements of your application. What do you expect from the random numbers "based" on the seed returned by a system tool? Do you expect unpredictability of the results? What are the requirements to stat properties of the random numbers used inthe application, and so forth? Having answers to these questions should help you to determine the right strategy for the seed generation.