<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic MKL random number stream equivalent to Matlab default RandStream in Intel® oneAPI Math Kernel Library</title>
    <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/MKL-random-number-stream-equivalent-to-Matlab-default-RandStream/m-p/999076#M18407</link>
    <description>&lt;P&gt;Is it possible to generate random numbers with MKL that are equivalent to Matlab random numbers?&amp;nbsp;&lt;/P&gt;

&lt;P&gt;I use the following Matlab/ Fortran codes but the results are different&lt;/P&gt;

&lt;PRE class="brush:bash;"&gt;Matlab
stream=RandStream('mt19937ar','Seed',0);
RandStream.setGlobalStream(stream);
reset(stream,0)

fid = fopen('~/Desktop/iseed.bin', 'w');
fwrite(fid, stream.State,'int32');
fclose(fid);

rand(10,1)

% 0.8147
% 0.9058
% 0.1270
% 0.9134
% 0.6324
% 0.0975
% 0.2785
% 0.5469
% 0.9575
% 0.9649&lt;/PRE&gt;

&lt;P&gt;Fortran&lt;/P&gt;

&lt;PRE class="brush:fortran;"&gt;include 'mkl_vsl.f90'
program main
USE MKL_VSL_TYPE
USE MKL_VSL
implicit none
integer :: params(625)

real(kind=8) r(10) ! buffer for random numbers
TYPE (VSL_STREAM_STATE) :: stream
integer(kind=4) :: errcode
integer(kind=4) :: i,j
integer :: brng,method,seed,n

open(1, file='~/Desktop/iseed.bin', form='binary')
read(1) params
close(1)

n = 10
brng=VSL_BRNG_MT19937
method=VSL_RNG_METHOD_UNIFORM_STD
seed=0
errcode=vslnewstreamex(stream, brng, 625, params)
! alternative option without matlab stream state
! errcode=vslnewstream( stream, brng, seed )
errcode=vdrnguniform(method, stream, n, r, 0.0d0, 1.0d0)
write(*,'(f)')  r(1:10)
errcode=vsldeletestream( stream )
end

! result
0.1327075199224055
0.3464016632642597
0.7798899088520557
0.4143710811622441
0.4759427784010768
0.4244252541102469
0.0815817557740957
0.9338225021492690
0.5113811327610165
0.5184877812862396&lt;/PRE&gt;</description>
    <pubDate>Fri, 19 Sep 2014 15:49:57 GMT</pubDate>
    <dc:creator>zuch</dc:creator>
    <dc:date>2014-09-19T15:49:57Z</dc:date>
    <item>
      <title>MKL random number stream equivalent to Matlab default RandStream</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/MKL-random-number-stream-equivalent-to-Matlab-default-RandStream/m-p/999076#M18407</link>
      <description>&lt;P&gt;Is it possible to generate random numbers with MKL that are equivalent to Matlab random numbers?&amp;nbsp;&lt;/P&gt;

&lt;P&gt;I use the following Matlab/ Fortran codes but the results are different&lt;/P&gt;

&lt;PRE class="brush:bash;"&gt;Matlab
stream=RandStream('mt19937ar','Seed',0);
RandStream.setGlobalStream(stream);
reset(stream,0)

fid = fopen('~/Desktop/iseed.bin', 'w');
fwrite(fid, stream.State,'int32');
fclose(fid);

rand(10,1)

% 0.8147
% 0.9058
% 0.1270
% 0.9134
% 0.6324
% 0.0975
% 0.2785
% 0.5469
% 0.9575
% 0.9649&lt;/PRE&gt;

&lt;P&gt;Fortran&lt;/P&gt;

&lt;PRE class="brush:fortran;"&gt;include 'mkl_vsl.f90'
program main
USE MKL_VSL_TYPE
USE MKL_VSL
implicit none
integer :: params(625)

real(kind=8) r(10) ! buffer for random numbers
TYPE (VSL_STREAM_STATE) :: stream
integer(kind=4) :: errcode
integer(kind=4) :: i,j
integer :: brng,method,seed,n

open(1, file='~/Desktop/iseed.bin', form='binary')
read(1) params
close(1)

n = 10
brng=VSL_BRNG_MT19937
method=VSL_RNG_METHOD_UNIFORM_STD
seed=0
errcode=vslnewstreamex(stream, brng, 625, params)
! alternative option without matlab stream state
! errcode=vslnewstream( stream, brng, seed )
errcode=vdrnguniform(method, stream, n, r, 0.0d0, 1.0d0)
write(*,'(f)')  r(1:10)
errcode=vsldeletestream( stream )
end

! result
0.1327075199224055
0.3464016632642597
0.7798899088520557
0.4143710811622441
0.4759427784010768
0.4244252541102469
0.0815817557740957
0.9338225021492690
0.5113811327610165
0.5184877812862396&lt;/PRE&gt;</description>
      <pubDate>Fri, 19 Sep 2014 15:49:57 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/MKL-random-number-stream-equivalent-to-Matlab-default-RandStream/m-p/999076#M18407</guid>
      <dc:creator>zuch</dc:creator>
      <dc:date>2014-09-19T15:49:57Z</dc:date>
    </item>
    <item>
      <title>Hi Zuch, </title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/MKL-random-number-stream-equivalent-to-Matlab-default-RandStream/m-p/999077#M18408</link>
      <description>&lt;P&gt;Hi Zuch,&amp;nbsp;&lt;/P&gt;

&lt;P&gt;Could you please also attach the file&amp;nbsp;&lt;SPAN style="color: rgb(0, 0, 255); font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; line-height: 14.3088006973267px; background-color: rgb(248, 248, 248);"&gt;~/Desktop/iseed.bin'. &amp;nbsp;?&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;

&lt;P&gt;&lt;FONT color="#0000ff" face="Consolas, Bitstream Vera Sans Mono, Courier New, Courier, monospace"&gt;&lt;SPAN style="line-height: 14.3088006973267px; background-color: rgb(248, 248, 248);"&gt;Thanks&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;

&lt;P&gt;&lt;FONT color="#0000ff" face="Consolas, Bitstream Vera Sans Mono, Courier New, Courier, monospace"&gt;&lt;SPAN style="line-height: 14.3088006973267px; background-color: rgb(248, 248, 248);"&gt;Ying&amp;nbsp;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Sep 2014 03:14:25 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/MKL-random-number-stream-equivalent-to-Matlab-default-RandStream/m-p/999077#M18408</guid>
      <dc:creator>Ying_H_Intel</dc:creator>
      <dc:date>2014-09-22T03:14:25Z</dc:date>
    </item>
    <item>
      <title>Hi Ying, I'am attaching seed</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/MKL-random-number-stream-equivalent-to-Matlab-default-RandStream/m-p/999078#M18409</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;SPAN style="font-size: 12px; line-height: 18px;"&gt;Ying&lt;/SPAN&gt;, I'am attaching seed state in binary form.&amp;nbsp;&lt;/P&gt;

&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Mon, 22 Sep 2014 09:15:58 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/MKL-random-number-stream-equivalent-to-Matlab-default-RandStream/m-p/999078#M18409</guid>
      <dc:creator>zuch</dc:creator>
      <dc:date>2014-09-22T09:15:58Z</dc:date>
    </item>
    <item>
      <title>Hi Zuch,</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/MKL-random-number-stream-equivalent-to-Matlab-default-RandStream/m-p/999079#M18410</link>
      <description>&lt;P&gt;Hi Zuch,&lt;/P&gt;

&lt;P&gt;What value of Seed do you provide into Matlab version of MT19937? Did you provide the same seed into MKL version of the generator?&lt;/P&gt;

&lt;P&gt;Initialization of the generator relies on the&amp;nbsp;algorithm which converts the seed (a scalar or array)&amp;nbsp;into its internal state. Thus, if you provide the array of 625 numbers into the generator, the initialization&amp;nbsp;does not take the array as its state, but transforms the array into another state.&lt;/P&gt;

&lt;P&gt;Additional details about initialization of MT19937 in MKL are available in VSL Notes, &lt;A href="https://software.intel.com/en-us/mkl_11.1_vslnotes"&gt;https://software.intel.com/en-us/mkl_11.1_vslnotes&lt;/A&gt;, the section "Testing of Basic Random Number Generators\ Basic Random Generator Properties and Testing Results \ MT19937".&lt;/P&gt;

&lt;P&gt;Please, let us know, if you have more questions and/or results of your further experiments with Matlab and MKL versions of MT19937 generator.&lt;/P&gt;

&lt;P&gt;Thanks,&lt;/P&gt;

&lt;P&gt;Andrey&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Sep 2014 06:12:42 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/MKL-random-number-stream-equivalent-to-Matlab-default-RandStream/m-p/999079#M18410</guid>
      <dc:creator>Andrey_N_Intel</dc:creator>
      <dc:date>2014-09-23T06:12:42Z</dc:date>
    </item>
    <item>
      <title>Thanks Andrey, I will read</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/MKL-random-number-stream-equivalent-to-Matlab-default-RandStream/m-p/999080#M18411</link>
      <description>&lt;P&gt;Thanks Andrey, I will read the details in VSL notes. &amp;nbsp;&lt;/P&gt;

&lt;P&gt;Re: seed value, I use 0 for both MKL and Matlab&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Sep 2014 21:52:22 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/MKL-random-number-stream-equivalent-to-Matlab-default-RandStream/m-p/999080#M18411</guid>
      <dc:creator>zuch</dc:creator>
      <dc:date>2014-09-23T21:52:22Z</dc:date>
    </item>
    <item>
      <title>Hi Zuch,</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/MKL-random-number-stream-equivalent-to-Matlab-default-RandStream/m-p/999081#M18412</link>
      <description>&lt;P&gt;Hi Zuch,&lt;/P&gt;

&lt;P&gt;If I correctly understand, you see different outputs between Intel MKL and Matlab versions of&amp;nbsp;MT19937 also in the case when you&amp;nbsp;provide the same&amp;nbsp;seed&amp;nbsp;(=0) into vslNewStream&amp;nbsp;routine. If this is the case, the possible reason might be in the initialization of the generator. MKL version of MT19937 relies on the scheme which corresponds to the initialization function init_by_array() in the original code &lt;A href="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/CODES/mt19937ar.c"&gt;http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/CODES/mt19937ar.c.&lt;/A&gt; That is, when you provide a scalar seed, it would be treated as array of size 1, and the state of the generator is initialized properly. While I'm not aware about&amp;nbsp;the initialization of the Matlab version of the generator for case of a scalar seed, it&amp;nbsp;might&amp;nbsp;rely on the&amp;nbsp;algorithm implemented in the init_genrand().&lt;/P&gt;

&lt;P&gt;Please, let me know, if you have more questions or need further help.&lt;/P&gt;

&lt;P&gt;Thanks,&lt;/P&gt;

&lt;P&gt;Andrey&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Sep 2014 06:37:05 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/MKL-random-number-stream-equivalent-to-Matlab-default-RandStream/m-p/999081#M18412</guid>
      <dc:creator>Andrey_N_Intel</dc:creator>
      <dc:date>2014-09-25T06:37:05Z</dc:date>
    </item>
    <item>
      <title>Hi Zuch, </title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/MKL-random-number-stream-equivalent-to-Matlab-default-RandStream/m-p/999082#M18413</link>
      <description>&lt;P&gt;Hi Zuch,&amp;nbsp;&lt;/P&gt;

&lt;P&gt;Any news regarding the problem?&amp;nbsp;&lt;/P&gt;

&lt;P&gt;The key is the algorithm of the initial array.&amp;nbsp;&lt;/P&gt;

&lt;P&gt;MKL use vector array, when scalar seed, it take it as 1 size array. &amp;nbsp;How about you try &amp;nbsp;the vector function:&amp;nbsp;&lt;SPAN style="font-family: Consolas, 'Lucida Console', Menlo, Monaco, 'DejaVu Sans Mono', monospace, sans-serif; font-size: 1em; line-height: 19.5120010375977px;"&gt;RandStream.create('',',1);&lt;/SPAN&gt;&lt;/P&gt;

&lt;P&gt;&lt;SPAN style="font-family: Consolas, 'Lucida Console', Menlo, Monaco, 'DejaVu Sans Mono', monospace, sans-serif; font-size: 1em; line-height: 19.5120010375977px;"&gt;same result?&lt;/SPAN&gt;&lt;/P&gt;

&lt;P&gt;Best Regards,&lt;BR /&gt;
	Ying&lt;/P&gt;

&lt;H3&gt;Example 1&lt;/H3&gt;

&lt;P&gt;Create a single stream and designate it as the current global stream:&lt;/P&gt;

&lt;PRE class="programlisting"&gt;s = RandStream('mt19937ar','Seed',1);
RandStream.setGlobalStream(s);&lt;/PRE&gt;

&lt;P&gt;&lt;A name="btq7b6x-2"&gt;&lt;/A&gt;&lt;/P&gt;

&lt;H3&gt;Example 2&lt;/H3&gt;

&lt;P&gt;Create three independent streams:&lt;/P&gt;

&lt;PRE class="programlisting"&gt;[s1,s2,s3] = RandStream.create('mrg32k3a','NumStreams',3);
r1 = rand(s1,100000,1);
&lt;/PRE&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 11 Oct 2014 04:03:10 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/MKL-random-number-stream-equivalent-to-Matlab-default-RandStream/m-p/999082#M18413</guid>
      <dc:creator>Ying_H_Intel</dc:creator>
      <dc:date>2014-10-11T04:03:10Z</dc:date>
    </item>
    <item>
      <title>Hi John,</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/MKL-random-number-stream-equivalent-to-Matlab-default-RandStream/m-p/999084#M18415</link>
      <description>&lt;P&gt;Hi John,&lt;/P&gt;

&lt;P&gt;if you need truly random seed you may want to&amp;nbsp;use non-deterministic generator from Intel MKL to produce it. The generator&amp;nbsp;provides interface to RDRAND instruction available on the latest CPUs. However, if you&amp;nbsp;generate&amp;nbsp;a new seed each time before simulation of the Wiener paths, you will run into non-reproducible results that might not be good for goals of your application.&amp;nbsp;What are the reasons you need a random seed that changes each time for simulation of the Wiener process paths? Why&amp;nbsp;other&amp;nbsp;initialization schemes (like&amp;nbsp;choosing&amp;nbsp;the seed&amp;nbsp;by means of, say,RDRAND and fixing it)&amp;nbsp;that allow getting reproducible&amp;nbsp;results do not work for you?&lt;/P&gt;

&lt;P&gt;Also, just in case, this link &lt;A href="https://software.intel.com/en-us/articles/initializing-Intel-MKL-BRNGs"&gt;https://software.intel.com/en-us/articles/initializing-Intel-MKL-BRNGs&lt;/A&gt; provides additional details on the initialization of Intel MKL generators.&lt;/P&gt;

&lt;P&gt;Thanks,&lt;/P&gt;

&lt;P&gt;Andrey&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 May 2016 06:37:23 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/MKL-random-number-stream-equivalent-to-Matlab-default-RandStream/m-p/999084#M18415</guid>
      <dc:creator>Andrey_N_Intel</dc:creator>
      <dc:date>2016-05-13T06:37:23Z</dc:date>
    </item>
    <item>
      <title> </title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/MKL-random-number-stream-equivalent-to-Matlab-default-RandStream/m-p/999083#M18414</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;

&lt;P&gt;I grabbed this code to generate a Weiner Process. The bin file is not in the least bit random, it repeats after about 6 steps, how do I generate a more random seed that actually changes each time.&amp;nbsp;&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 May 2016 20:59:44 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/MKL-random-number-stream-equivalent-to-Matlab-default-RandStream/m-p/999083#M18414</guid>
      <dc:creator>JohnNichols</dc:creator>
      <dc:date>2016-05-12T20:59:44Z</dc:date>
    </item>
    <item>
      <title>Thanks -- the problem is I am</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/MKL-random-number-stream-equivalent-to-Matlab-default-RandStream/m-p/999085#M18416</link>
      <description>&lt;P&gt;Thanks -- the problem is I am trying to get huge data sets that are truly random as this is modelling the flickering thermal input to a timber beam.&amp;nbsp; The interesting element is the FFT of the actual data shows for constant frequency points a Generalized Poissonian distribution in the results.&amp;nbsp; I prefer to know the points are not reproduced from run to run as I want to see the underlying stuff well and see the effect of the randomness of the process.&lt;/P&gt;

&lt;P&gt;John&lt;/P&gt;</description>
      <pubDate>Fri, 13 May 2016 13:46:50 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-Math-Kernel-Library/MKL-random-number-stream-equivalent-to-Matlab-default-RandStream/m-p/999085#M18416</guid>
      <dc:creator>JohnNichols</dc:creator>
      <dc:date>2016-05-13T13:46:50Z</dc:date>
    </item>
  </channel>
</rss>

