<?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 Re: random numbers generated by rand function inconsistent with the original mersenne twister code in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/random-numbers-generated-by-rand-function-inconsistent-with-the/m-p/328848#M271745</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;: This DATA step does not generate many independent streams. It creates one long stream from&amp;nbsp;seed=1. See &lt;A href="http://blogs.sas.com/content/iml/2011/08/31/random-number-streams-in-sas-how-do-they-work.html" target="_self"&gt;"Random number streams in SAS: How do they work?"&lt;/A&gt;, which includes the statement "It does not make sense to call STREAMINIT multiple times within the same DATA step; after the RAND function is called, subsequent calls to STREAMINIT are ignored. "&lt;/P&gt;</description>
    <pubDate>Tue, 31 Jan 2017 18:47:28 GMT</pubDate>
    <dc:creator>Rick_SAS</dc:creator>
    <dc:date>2017-01-31T18:47:28Z</dc:date>
    <item>
      <title>random numbers generated by rand function inconsistent with the original mersenne twister code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/random-numbers-generated-by-rand-function-inconsistent-with-the/m-p/328701#M271740</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to figure out why I'm getting different answers between the original C-implementation of the Mersenne-Twister algorithm and the rand function in SAS.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I run the C-code (from here: &lt;A href="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html" target="_blank"&gt;http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html&lt;/A&gt;), with a seed of &lt;STRONG&gt;5489&lt;/STRONG&gt; the first five random numbers generated are:&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;3499211612&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 581869302&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3890346734&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3586334585&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 545404204&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;This produces unsigned 32-bit integers. &amp;nbsp;To get something that I can compare with rand('uniform') in SAS I've just divided by 4294967295 = (2^32-1), which gives:&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;0.814723692, &amp;nbsp;0.135477004, &amp;nbsp;0.905791934, &amp;nbsp;0.83500859, 0.12698681 &amp;nbsp; &amp;nbsp;("series 1")&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I've then run the following data step in SAS with the intention of getting the same as series 1:&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data A;
call streaminit(5489);       /* set random number seed */
do i = 1 to 5;
   u = rand("Uniform");     /* u ~ U(0,1) */
   output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But this generates the following sequence:&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;0.204043448, &amp;nbsp;0.4575133489,&amp;nbsp;0.3245377645,&amp;nbsp;0.1522461593,&amp;nbsp;0.6659437388&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I'm a bit stumped as to why these are different. &amp;nbsp;I've looked at the SAS documentation on the rand function already (&lt;A href="http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a001466748.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a001466748.htm&lt;/A&gt;), but it doesn't help.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The version of SAS I'm using is 9.3 (32 bit).&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Very grateful for any suggestions!&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thanks&lt;BR /&gt;Michael&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 31 Jan 2017 11:30:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/random-numbers-generated-by-rand-function-inconsistent-with-the/m-p/328701#M271740</guid>
      <dc:creator>michaelT</dc:creator>
      <dc:date>2017-01-31T11:30:11Z</dc:date>
    </item>
    <item>
      <title>Re: random numbers generated by rand function inconsistent with the original mersenne twister code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/random-numbers-generated-by-rand-function-inconsistent-with-the/m-p/328760#M271741</link>
      <description>&lt;P&gt;First stop: numeric precision&lt;/P&gt;
&lt;P&gt;Second likely: the Seed value usage may vary between the algorithms&lt;/P&gt;
&lt;P&gt;And just because the algorithm is the same I dont' think I would expect the exact same series between different implementations.&lt;/P&gt;</description>
      <pubDate>Tue, 31 Jan 2017 15:10:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/random-numbers-generated-by-rand-function-inconsistent-with-the/m-p/328760#M271741</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-01-31T15:10:59Z</dc:date>
    </item>
    <item>
      <title>Re: random numbers generated by rand function inconsistent with the original mersenne twister code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/random-numbers-generated-by-rand-function-inconsistent-with-the/m-p/328785#M271742</link>
      <description>&lt;P&gt;Thanks ballardw.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Re: 1) numerical precision. &amp;nbsp;Yes, this could well be a cause. &amp;nbsp;I don't know how SAS handles things like integers. &amp;nbsp;My guess is that SAS might not use 'integers',&amp;nbsp;as you don't have to declare data types in SAS (as you do in C). &amp;nbsp;But I'd be a little surprised if it couldn't store integers (which are all that is used through the MT algortihm) without loss of precision.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Re: 2) Seed value usage. &amp;nbsp;Interesting - I think you're suggesting that &amp;nbsp;SAS might do something with the seed supplied to create a new seed from the seed it's been given? &amp;nbsp;From what I've read the mersenne twister ideally needs a few iterations to really get going into (pseudo) randomness, so maybe SAS does s/thing to take account of this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If anyone knows any further details about how MT is implemented in SAS, that would be really helpful. &amp;nbsp;If it is around seed value usage (i.e. 2 above) then that gives me hope that I could recreate, given details of what SAS is doing. &amp;nbsp;But at the moment it's just a black box where the output doesn't match what I'd like!&lt;/P&gt;</description>
      <pubDate>Tue, 31 Jan 2017 15:53:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/random-numbers-generated-by-rand-function-inconsistent-with-the/m-p/328785#M271742</guid>
      <dc:creator>michaelT</dc:creator>
      <dc:date>2017-01-31T15:53:10Z</dc:date>
    </item>
    <item>
      <title>Re: random numbers generated by rand function inconsistent with the original mersenne twister code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/random-numbers-generated-by-rand-function-inconsistent-with-the/m-p/328803#M271743</link>
      <description>&lt;P&gt;My suggestion about precision has more to do with the results of the division. Did you do the division in SAS or some other program?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The seed you used in the call streaminit just generates a reproducible stream from the rand function. It does not necessarily start at the position in the sequence of values the code in C would start with.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;An exercise for the interested reader would be to test all of the 2**31 -1 acceptable values of seed to see if any of them match the series (or somewhat closely) you seem interested in.&lt;/P&gt;
&lt;P&gt;This should generate a big data set of values with different streams:&lt;/P&gt;
&lt;PRE&gt;data mtseeds;
   do seed= 1 to 2147483647;
      if seed=1 then do;
         call execute("data mtseeds;call streaminit("||seed||
         ");
          array s {5};
          do i= 1 to 5;
            s[i] = rand('uniform');
          end;
          output;
          drop i;
          run;");
      end;
      else do;
         call execute("data temp;call streaminit("||seed||
         ");
          array s {5};
          do i= 1 to 5;
            s[i] = rand('uniform');
          end;
          output;
          drop i;
          run;
          Proc append base=mtseeds data=temp;run;");

      end;
   end;
run;&lt;/PRE&gt;
&lt;P&gt;First try that doesn't work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data mtseeds;
   array s {5};
   do seed= 1 to 2147483647;
      call streaminit(seed);
      do i= 1 to 5;
         s[i] = rand('uniform');
      end;
      output;/* or put a test for your expected 5 values and output if the delta is "small"*/
   end;
   drop i;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However since the period of the MT is 2**19937 -1 the odds seem somewhat unlikely.&lt;/P&gt;</description>
      <pubDate>Tue, 31 Jan 2017 19:03:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/random-numbers-generated-by-rand-function-inconsistent-with-the/m-p/328803#M271743</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-01-31T19:03:59Z</dc:date>
    </item>
    <item>
      <title>Re: random numbers generated by rand function inconsistent with the original mersenne twister code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/random-numbers-generated-by-rand-function-inconsistent-with-the/m-p/328823#M271744</link>
      <description>&lt;P&gt;I just did the division in excel - but given the output from SAS is so far apart from my series 1 I don't think that is an issue. &amp;nbsp;(I think that I am missing your point here, so apologies).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your suggestion about trying to figure out where SAS is starting from is an interesting one, thanks. &amp;nbsp;The MT state vector is transformed ("twisted") after each 624 random numbers from it, producing a new MT state vector, which then produces another 624 random numbers and is then twisted again, and so on. &amp;nbsp;So I guess I could &amp;nbsp;take the first 624 random numbers produced from SAS and convert into integers (and maybe here rounding is a problem) by multiplying by 2^32 - 1. &amp;nbsp;And if I could use this state vector as the starting point in the C-code it would at least confirm that the SAS implementation of MT is doing the same thing as in C, given the same MT vector state. &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Though even if I did that it still leaves the problem of what SAS is doing at the start with the seed that is different from the C-implementation. &amp;nbsp;I suspect that is not easy to crack without further information.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anyway, thanks for your thoughts on this.&lt;/P&gt;</description>
      <pubDate>Tue, 31 Jan 2017 17:14:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/random-numbers-generated-by-rand-function-inconsistent-with-the/m-p/328823#M271744</guid>
      <dc:creator>michaelT</dc:creator>
      <dc:date>2017-01-31T17:14:42Z</dc:date>
    </item>
    <item>
      <title>Re: random numbers generated by rand function inconsistent with the original mersenne twister code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/random-numbers-generated-by-rand-function-inconsistent-with-the/m-p/328848#M271745</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;: This DATA step does not generate many independent streams. It creates one long stream from&amp;nbsp;seed=1. See &lt;A href="http://blogs.sas.com/content/iml/2011/08/31/random-number-streams-in-sas-how-do-they-work.html" target="_self"&gt;"Random number streams in SAS: How do they work?"&lt;/A&gt;, which includes the statement "It does not make sense to call STREAMINIT multiple times within the same DATA step; after the RAND function is called, subsequent calls to STREAMINIT are ignored. "&lt;/P&gt;</description>
      <pubDate>Tue, 31 Jan 2017 18:47:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/random-numbers-generated-by-rand-function-inconsistent-with-the/m-p/328848#M271745</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2017-01-31T18:47:28Z</dc:date>
    </item>
  </channel>
</rss>

