{"id":2945,"date":"2012-09-26T14:49:04","date_gmt":"2012-09-26T13:49:04","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=2945"},"modified":"2012-10-08T05:04:19","modified_gmt":"2012-10-08T04:04:19","slug":"randomness-in-matlab-are-you-ruining-your-results-due-to-your-choice-of-syntax","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=2945","title":{"rendered":"Randomness in MATLAB : Are you ruining your results due to your choice of syntax?"},"content":{"rendered":"<p>Pop quiz: What does the following line of MATLAB code do?<\/p>\n<pre>rand('state',10)<\/pre>\n<p>If you said<em> &#8216;It changes the seed of the random number generator to 10&#8217;<\/em> you get half a point.<\/p>\n<p><em>&#8216;Only half a point!?&#8217;<\/em> I hear you say accusingly <em>&#8216;but it says so in my book [for example, 1-3], why not a full point?&#8217;<\/em><\/p>\n<p>You only get a full point if you&#8217;d said something like <em>&#8216;It changes the seed of the random number generator to 10 <strong>and it also changes the random number generator from the high quality, default Mersenne Twister generator to a lower quality legacy random number generator.<\/strong>&#8216;<\/em><\/p>\n<p>OK, how about this one?<\/p>\n<pre>rand('seed',10)<\/pre>\n<p>This behaves in a very similar manner&#8211; it changes both the seed and the type of the underlying generator. However, the random number generator it switches to this time is an even older one that was introduced as far back as MATLAB version 4.\u00a0 It is not very good at all by modern standards!<\/p>\n<p><strong>A closer look<\/strong><\/p>\n<p>Open up a fresh copy of a recent version of MATLAB and ask it about the random number generator it&#8217;s using<\/p>\n<pre>&gt;&gt; RandStream.getGlobalStream\r\nans =\r\nmt19937ar random stream (current global stream)\r\n             Seed: 0\r\n  NormalTransform: Ziggurat<\/pre>\n<p>mt1993ar refers to a particular variant of the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Mersenne_twister\">Mersenne Twister algorithm<\/a>&#8212; an industry strength random number generator that&#8217;s used in many software packages and simulations.\u00a0 It&#8217;s been the default generator in MATLAB since 2007a.\u00a0 Change the seed using the modern (since 2011a), recommended syntax and ask again:<\/p>\n<pre>&gt;&gt; rng(10)\r\n&gt;&gt; RandStream.getGlobalStream\r\nans =\r\nmt19937ar random stream (current global stream)\r\n             Seed: 10\r\n  NormalTransform: Ziggurat<\/pre>\n<p>This is behaving exactly as you&#8217;d expect, you ask it to change the seed and it changes the seed&#8230;nothing more, nothing less. Now, let&#8217;s use the older syntax<\/p>\n<pre>&gt;&gt; rand('state',10)\r\n&gt;&gt; RandStream.getGlobalStream\r\nans =\r\nlegacy random stream (current global stream)\r\n  RAND algorithm: V5 (Subtract-with-Borrow), RANDN algorithm: V5 (Ziggurat)<\/pre>\n<p>The random number generator has completely changed!\u00a0\u00a0 We are no longer using the Mersenne Twister algorithm, we are now using a &#8216;subtract with borrow&#8217; [see reference 4 for implementation details] generator which has been shown to have several undesirable issues [5-7].<\/p>\n<p>Let&#8217;s do it again but this time using the even older &#8216;seed&#8217; version:<\/p>\n<pre>&gt;&gt; rand('seed',10)\r\n&gt;&gt; RandStream.getGlobalStream\r\nans =\r\nlegacy random stream (current global stream)\r\n  RAND algorithm: V4 (Congruential), RANDN algorithm: V5 (Ziggurat)<\/pre>\n<p>Now, this random number generator is ancient by computing standards.\u00a0 It also has a relatively tiny period of only 2 billion or so.\u00a0 For details see [4]<\/p>\n<p><strong>Why this matters<\/strong><\/p>\n<p>Now, all of this is <a href=\"http:\/\/www.mathworks.co.uk\/help\/matlab\/math\/updating-your-random-number-generator-syntax.html\">well documented<\/a> so you may wonder why I am making such a big issue out of it.\u00a0 Here are my reasons<\/p>\n<ul>\n<li>I often get sent MATLAB code for the purposes of code-review and optimisation.\u00a0 I see the old seeding syntax a LOT and the program&#8217;s authors are often blissfully unaware of the consequnces.<\/li>\n<li>The old syntax looks like all it should do is change the seed.\u00a0 It doesn&#8217;t!\u00a0 Before 2007a, however, it did!<\/li>\n<li>The old syntax is written in dozens of books because it was once the default, correct syntax to use.<\/li>\n<li>Many users don&#8217;t read the <a href=\"http:\/\/www.mathworks.co.uk\/help\/matlab\/math\/updating-your-random-number-generator-syntax.html\">relevent section<\/a> of the MATLAB documentation because they have no idea that there is a potential issue.\u00a0 They read a book or tutorial..it says to use rand(&#8216;state&#8217;,10) so they do.<\/li>\n<li>MATLAB doesn&#8217;t use the old generators by default any more because they are not very good [4-7]!<\/li>\n<li>Using these old generators may adversely affect the quality of your simulation.<\/li>\n<\/ul>\n<p><strong>The bottom line<\/strong><\/p>\n<p>Don&#8217;t do either of these to change the seed of the default generator to 10:<\/p>\n<pre>rand('state',10)\r\nrand('seed',10)<\/pre>\n<p>Do this instead:<\/p>\n<pre>rng(10)<\/pre>\n<p>Only if you completely understand and accept the consequences of the older syntax should you use it.<\/p>\n<p><strong>References<\/strong><\/p>\n<p>1. &#8216;MATLAB &#8211; A practical introduction to programming and problem solving&#8217;, 2009,Stormy Attaway<\/p>\n<p>2. MATLAB Guide (Second Edition), 2005, Desmond Higham and Nicholas Higham<\/p>\n<p>3. Essential MATLAB for Engineers and Scientists (Fourth Edition), 2009, Hahn and Valentine<\/p>\n<p>4. Numerical Computing with MATLAB, 2004, Cleve Moler (<a href=\"http:\/\/www.mathworks.co.uk\/moler\/chapters.html\">available online<\/a>)<\/p>\n<p>5.\u00a0 <a href=\"http:\/\/www.mathworks.co.uk\/support\/solutions\/en\/data\/1-10HYAS\/?solution=1-10HYAS\">Why does the random number generator in MATLAB fail a particular test of randomness?<\/a> The Mathworks, retreived 26th September 2012<\/p>\n<p>6. <a href=\"http:\/\/www2.cs.cas.cz\/~savicky\/papers\/rand2006.pdf\">A strong nonrandom pattern in Matlab default random number generator<\/a>, 2006, Petr Savicky, retreived 26th September 2012<\/p>\n<p>7.\u00a0 Learning Random Numbers: A Matlab Anomaly, 2008, Petr Savicky and Marko Robnik-\u0160ikonja, Applied Artificial Intelligence, Vol22 issue 3, pp 254-265<\/p>\n<p><strong>Other posts on random numbers in MATLAB<\/strong><\/p>\n<ul>\n<li><a href=\"https:\/\/www.walkingrandomly.com\/?p=2755\">Parallel Random Numbers in MATLAB #1<\/a> &#8211; An introduction to random numbers and seeding in MATLAB<\/li>\n<li><a href=\"https:\/\/www.walkingrandomly.com\/?p=3555\">Probability of overlapping subsequences<\/a> (How likely is it to get overlapping random sequences using two different seeds for the Mersenne Twister algorithm)<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Pop quiz: What does the following line of MATLAB code do? rand(&#8216;state&#8217;,10) If you said &#8216;It changes the seed of the random number generator to 10&#8217; you get half a point. &#8216;Only half a point!?&#8217; I hear you say accusingly &#8216;but it says so in my book [for example, 1-3], why not a full point?&#8217; [&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":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,11,7,63],"tags":[],"class_list":["post-2945","post","type-post","status-publish","format-standard","hentry","category-math-software","category-matlab","category-programming","category-random-numbers"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-Lv","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/2945","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=2945"}],"version-history":[{"count":10,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/2945\/revisions"}],"predecessor-version":[{"id":4630,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/2945\/revisions\/4630"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}