Quantcast
Channel: Intel® Software - Intel® oneAPI Math Kernel Library & Intel® Math Kernel Library
Viewing all articles
Browse latest Browse all 3005

MKL random number stream equivalent to Matlab default RandStream

$
0
0

Is it possible to generate random numbers with MKL that are equivalent to Matlab random numbers? 

I use the following Matlab/ Fortran codes but the results are different

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

Fortran

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

Viewing all articles
Browse latest Browse all 3005

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>