{"id":5480,"date":"2014-06-18T10:51:54","date_gmt":"2014-06-18T09:51:54","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=5480"},"modified":"2014-06-18T10:51:54","modified_gmt":"2014-06-18T09:51:54","slug":"matlabs-mersenne-twister-random-number-generator-seed-0-gives-the-same-numbers-as-seed-5489","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=5480","title":{"rendered":"MATLAB&#8217;s Mersenne Twister Random Number Generator: Seed 0 gives the same numbers as Seed 5489"},"content":{"rendered":"<p>Something that became clear from my recent <a href=\"https:\/\/www.walkingrandomly.com\/?p=5479\">comparison of Numpy&#8217;s Mersenne Twister implementation with MATLAB&#8217;s<\/a> is that there is something funky going on with seed 0 in MATLAB. A <a href=\"https:\/\/www.walkingrandomly.com\/?p=5479#comment-1888285\">discussion in the comments thread<\/a> helped uncover what was going on. In short, seed 0 gives exactly the same random numbers as seed 5489 in MATLAB (unless you use their deprecated rand(&#8216;twister&#8217;,0) syntax).<\/p>\n<p>This is a potential problem for anyone who performs lots of simulations that make use of random numbers such as monte-carlo simulations. One common work-flow is to run the same program hundreds of times where only the seed differs between runs. This is probably good enough to ensure that each simulation uses a random number stream that is statistically independent from all of the others &#8212; There is a risk that some streams will overlap but the <a href=\"https:\/\/www.walkingrandomly.com\/?p=3555\">probability is low<\/a> and most people are content to live with that risk.<\/p>\n<p>The practical upshot of this is that if you intend on sticking with Mersenne Twister for your MATLAB monte-carlo simulations, it might be wise to avoid seed 0. Alternatively, move to a random number generator that guarantees non-overlapping, independent streams &#8211; something that any implementation of Mersenne Twister cannot do.<\/p>\n<p>Here&#8217;s a demo run in MATLAB 2014a on Windows 7.<\/p>\n<pre>&gt;&gt; format long\r\n&gt;&gt; rng(0)\r\n&gt;&gt; rand(1,5)'\r\n\r\nans =\r\n\r\n   0.814723686393179\r\n   0.905791937075619\r\n   0.126986816293506\r\n   0.913375856139019\r\n   0.632359246225410\r\n\r\n&gt;&gt; rng(5489)\r\n&gt;&gt; rand(1,5)'\r\n\r\nans =\r\n\r\n   0.814723686393179\r\n   0.905791937075619\r\n   0.126986816293506\r\n   0.913375856139019\r\n   0.632359246225410<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Something that became clear from my recent comparison of Numpy&#8217;s Mersenne Twister implementation with MATLAB&#8217;s is that there is something funky going on with seed 0 in MATLAB. A discussion in the comments thread helped uncover what was going on. In short, seed 0 gives exactly the same random numbers as seed 5489 in MATLAB [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,11,63],"tags":[],"class_list":["post-5480","post","type-post","status-publish","format-standard","hentry","category-math-software","category-matlab","category-random-numbers"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-1qo","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5480","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5480"}],"version-history":[{"count":1,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5480\/revisions"}],"predecessor-version":[{"id":5490,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/5480\/revisions\/5490"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5480"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5480"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5480"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}