title: C# Mersenne Twister random integer generator implementation (SFMT) monte carlo simulation
body: <p>So far I've been using the C# <a href="http://en.wikipedia.org/wiki/Mersenne_twister" rel="noreferrer">Mersenne Twister</a> found here to generate random numbers:</p>
  
  <p><a href="http://www.centerspace.net/resources.php" rel="noreferrer">http://www.centerspace.net/resources.php</a></p>
  
  <p>I just discovered <strong><a href="http://en.wikipedia.org/wiki/Mersenne_twister#SFMT" rel="noreferrer">SFMT</a></strong> which is supposed to be twice as fast here:</p>
  
  <p><a href="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/" rel="noreferrer">http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/</a></p>
  
  <p><strong>Can anyone point me at a C# implementation of SFMT</strong>?</p>
  
  <p>My requirements are to generate an integer between (and including) 0 and 2^20 (1048576).</p>
  
  <p>I need to do this <strong>trillions of times everyday</strong> for a simulation running on a 24 hour clock so I am prepared to spend days tweaking this to perfection.</p>
  
  <p>Currently I've tweaked the Center Space Mersenne Twister by adding a new method to fit my requirements:</p>
  
  <pre><code>public uint Next20()
  {            
      return (uint)(genrand_int32() &gt;&gt; 12);
  }
  </code></pre>
  
  <p>Using the method <code>genrand_int32()</code> I'd like to produce my own version, <code>genrand_int20()</code>, that generates an integer between (and including) 0 and 2^20 to save on the <strong>cast above and shift</strong> but I don't understand the mathematics. Exactly how can I do this?</p>
  
  <p>Also is using an <strong>uint going to be faster that int</strong>, or is just a matter of addressable numbers? Because I only need up to 1048576, I am only concerned with speed.</p>
  
  <p>Also this will be running on a <strong>Windows Server 2003 R2 SP2 (32bit) box with .NET 2. Processor is <a href="http://en.wikipedia.org/wiki/List_of_AMD_Opteron_microprocessors#Opteron_200-series_.22Italy.22_.28E6.2C_90_nm.29" rel="noreferrer">AMD Opteron 275</a> (4 core)</strong>.</p>