<?xml version="1.0"?><?xml-stylesheet type="text/xsl" href="http://code.msdn.microsoft.com/rss.xsl"?><rss version="2.0"><channel><title>Better random numbers in .Net: an updated Mersenne Twister in C#</title><link>http://code.msdn.microsoft.com/MersenneTwister/Project/ProjectRss.aspx</link><description>The Mersenne Twister is a rather well-known algorithm for generating pseudo-random numbers. It is fast and has a huge period &amp;#40;range over which it doesn&amp;#39;t repeat itself&amp;#41;, which makes it ideal for si...</description><item><title>NEW POST: Check out the translation to VB</title><link>http://code.msdn.microsoft.com/MersenneTwister/Thread/View.aspx?ThreadId=317</link><description>&lt;div class="wikidoc"&gt;
Justin Michel translated this to &lt;a href="http://code.msdn.microsoft.com/MersenneTwister/WorkItem/AttachmentDownload.ashx?WorkItemId=1&amp;amp;FileAttachmentId=1675" class="externalLink"&gt;VB.Net here&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;.&lt;br /&gt;
&lt;/div&gt;</description><author>codekaizen</author><pubDate>Sat, 26 Apr 2008 04:08:47 GMT</pubDate><guid isPermaLink="false">NEW POST: Check out the translation to VB 20080426A</guid></item><item><title>CLOSED ISSUE: Broken Next()?</title><link>http://code.msdn.microsoft.com/MersenneTwister/WorkItem/View.aspx?WorkItemId=1</link><description>I ported the code to VB &amp;#40;used an automated tool followed by minor modifications&amp;#41;. As a sanity check I wrote a simple program to compute randoms for a few seconds, count distributions, and then dump to the console.&lt;br /&gt;&lt;br /&gt;I found that rand.Next&amp;#40;N&amp;#41; will always return half of the results that should be zero as N. This is an error, as Next&amp;#40;N&amp;#41; should always return a number less than N. &lt;br /&gt;&lt;br /&gt;For example, If I generate 30 randoms using rand.Next&amp;#40;3&amp;#41;, then I&amp;#39;ll get something like the following.&lt;br /&gt;0&amp;#61;5&lt;br /&gt;1&amp;#61;10&lt;br /&gt;2&amp;#61;10&lt;br /&gt;3&amp;#61;5&lt;br /&gt;&lt;br /&gt;It&amp;#39;s always possible that I messed something up in translation from C&amp;#35; to VB, but I couldn&amp;#39;t spot anything obvious.&lt;br /&gt;</description><author>codekaizen</author><pubDate>Sat, 26 Apr 2008 04:05:19 GMT</pubDate><guid isPermaLink="false">CLOSED ISSUE: Broken Next()? 20080426A</guid></item><item><title>COMMENTED ISSUE: Broken Next()?</title><link>http://code.msdn.microsoft.com/MersenneTwister/WorkItem/View.aspx?WorkItemId=1</link><description>I ported the code to VB &amp;#40;used an automated tool followed by minor modifications&amp;#41;. As a sanity check I wrote a simple program to compute randoms for a few seconds, count distributions, and then dump to the console.&lt;br /&gt;&lt;br /&gt;I found that rand.Next&amp;#40;N&amp;#41; will always return half of the results that should be zero as N. This is an error, as Next&amp;#40;N&amp;#41; should always return a number less than N. &lt;br /&gt;&lt;br /&gt;For example, If I generate 30 randoms using rand.Next&amp;#40;3&amp;#41;, then I&amp;#39;ll get something like the following.&lt;br /&gt;0&amp;#61;5&lt;br /&gt;1&amp;#61;10&lt;br /&gt;2&amp;#61;10&lt;br /&gt;3&amp;#61;5&lt;br /&gt;&lt;br /&gt;It&amp;#39;s always possible that I messed something up in translation from C&amp;#35; to VB, but I couldn&amp;#39;t spot anything obvious.&lt;br /&gt;Comments: Thanks for the translation&amp;#33;</description><author>codekaizen</author><pubDate>Fri, 18 Apr 2008 07:55:14 GMT</pubDate><guid isPermaLink="false">COMMENTED ISSUE: Broken Next()? 20080418A</guid></item><item><title>COMMENTED ISSUE: Broken Next()?</title><link>http://code.msdn.microsoft.com/MersenneTwister/WorkItem/View.aspx?WorkItemId=1</link><description>I ported the code to VB &amp;#40;used an automated tool followed by minor modifications&amp;#41;. As a sanity check I wrote a simple program to compute randoms for a few seconds, count distributions, and then dump to the console.&lt;br /&gt;&lt;br /&gt;I found that rand.Next&amp;#40;N&amp;#41; will always return half of the results that should be zero as N. This is an error, as Next&amp;#40;N&amp;#41; should always return a number less than N. &lt;br /&gt;&lt;br /&gt;For example, If I generate 30 randoms using rand.Next&amp;#40;3&amp;#41;, then I&amp;#39;ll get something like the following.&lt;br /&gt;0&amp;#61;5&lt;br /&gt;1&amp;#61;10&lt;br /&gt;2&amp;#61;10&lt;br /&gt;3&amp;#61;5&lt;br /&gt;&lt;br /&gt;It&amp;#39;s always possible that I messed something up in translation from C&amp;#35; to VB, but I couldn&amp;#39;t spot anything obvious.&lt;br /&gt;Comments: Well, my background threads finally caught up, and reminded me that VB always uses bankers rounding instead of truncation when converting from floating point to integer. &amp;#13;&amp;#10;Once I fixed the code, it works perfectly.&amp;#13;&amp;#10;Sorry for the confusion,&amp;#13;&amp;#10;j</description><author>JustinMichel</author><pubDate>Wed, 16 Apr 2008 17:50:13 GMT</pubDate><guid isPermaLink="false">COMMENTED ISSUE: Broken Next()? 20080416P</guid></item><item><title>CREATED ISSUE: Broken Next()?</title><link>http://code.msdn.microsoft.com/MersenneTwister/WorkItem/View.aspx?WorkItemId=1</link><description>I ported the code to VB &amp;#40;used an automated tool followed by minor modifications&amp;#41;. As a sanity check I wrote a simple program to compute randoms for a few seconds, count distributions, and then dump to the console.&lt;br /&gt;&lt;br /&gt;I found that rand.Next&amp;#40;N&amp;#41; will always return half of the results that should be zero as N. This is an error, as Next&amp;#40;N&amp;#41; should always return a number less than N. &lt;br /&gt;&lt;br /&gt;For example, If I generate 30 randoms using rand.Next&amp;#40;3&amp;#41;, then I&amp;#39;ll get something like the following.&lt;br /&gt;0&amp;#61;5&lt;br /&gt;1&amp;#61;10&lt;br /&gt;2&amp;#61;10&lt;br /&gt;3&amp;#61;5&lt;br /&gt;&lt;br /&gt;It&amp;#39;s always possible that I messed something up in translation from C&amp;#35; to VB, but I couldn&amp;#39;t spot anything obvious.&lt;br /&gt;</description><author>JustinMichel</author><pubDate>Mon, 14 Apr 2008 00:16:06 GMT</pubDate><guid isPermaLink="false">CREATED ISSUE: Broken Next()? 20080414A</guid></item><item><title>UPDATED WIKI: Home</title><link>http://code.msdn.microsoft.com/MersenneTwister/Wiki/View.aspx?title=Home&amp;version=6</link><description>&lt;div class="wikidoc"&gt;
The Mersenne Twister is a rather well-known algorithm for generating pseudo-random numbers. It is fast and has a huge period &amp;#40;range over which it doesn&amp;#39;t repeat itself&amp;#41;, which makes it ideal for simulation. However, the sequence can be guessed after only 624 numbers have been generated, so it is not useful for cryptography. It was first published in 1998 by Makoto Matsumoto and Takuji Nishimura using C code. 
*&lt;br /&gt; &lt;br /&gt;An acquaintance of mine, Trevor Misfeldt, converted the C code to C# and published it to &lt;a href="http://www.c-sharpcorner.com/uploadfile/trevormisfeldt/mersennetwisteralgo08302005002422am/mersennetwisteralgo.aspx" class="externalLink"&gt;c-sharpcorner&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. Another implementation in C# is from Akihilo Kramot at &lt;a href="http://www.takel.jp/mt/MersenneTwister.cs" class="externalLink"&gt;http://www.takel.jp/mt/MersenneTwister.cs&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;.&lt;br /&gt; &lt;br /&gt;I've taken both of these and created this version, since Trevor's is a fairly straight conversion from the C-code, yet has some compelling methods, and Akihilo Kramot's implementation is clean and well-adapted to .Net (implements System.Random), but contains a small bug in the NextBytes method.&lt;br /&gt; &lt;br /&gt;I have also implemented the 53-bit variety of the algorithm for the NextDouble* methods after an &lt;a href="http://www.springerlink.com/content/p5330n4932063068" class="externalLink"&gt;article &lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; was published in the journal &lt;i&gt;Monte Carlo Methods and Applications&lt;/i&gt;, Vol. 12, No. 5-6, pp. 385 – 393 (2006), entitled &amp;quot;A Repetition Test for Pseudo-Random Number Generators&amp;quot; highlighting that, somewhat unsurprisingly, the 32-bit variety for double floating points makes numbers repeat too soon in a sequence, whereas the 53-bit variety does not.&lt;br /&gt; &lt;br /&gt;This code is currently being used in the &lt;a href="http://www.codeplex.com/NPack" class="externalLink"&gt;NPack&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; library to provide random numbers for the numerical algorithms there. That project also has rudimentary unit tests covering this code for sanity checking (testing correct parameter guarding, primarily). I also hope to add at least one randomness test in order to verify the implementation (the algorithm has already tested).&lt;br /&gt; &lt;br /&gt;For the interested, more information on the Mersenne Twister can be found on &lt;a href="http://en.wikipedia.org/wiki/Mersenne_twister" class="externalLink"&gt;Wackypedia&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and appears, as of this posting, fairly accurate and useful. This could, however, change at any moment.&lt;br /&gt; &lt;br /&gt;* Published in the ACM's Transactions on Modeling and Computer Simulation Vol. 8, No. 1, January pp.3-30 (1998).&lt;br /&gt;
&lt;/div&gt;</description><author>codekaizen</author><pubDate>Wed, 27 Feb 2008 01:24:43 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: Home 20080227A</guid></item><item><title>CREATED RELEASE: 1.1 (Feb 26, 2008)</title><link>http://code.msdn.microsoft.com/MersenneTwister/Release/ProjectReleases.aspx?ReleaseId=529</link><description>Improved code factoring&lt;br /&gt;Improved code comments&lt;br /&gt;Changed names of NextFloat&amp;#42; methods to NextSingle&amp;#42;</description><author></author><pubDate>Tue, 26 Feb 2008 22:59:28 GMT</pubDate><guid isPermaLink="false">CREATED RELEASE: 1.1 (Feb 26, 2008) 20080226P</guid></item><item><title>UPDATED RELEASE: 1.0 (Feb 20, 2008)</title><link>http://code.msdn.microsoft.com/MersenneTwister/Release/ProjectReleases.aspx?ReleaseId=439</link><description>First release of the class.</description><author></author><pubDate>Fri, 22 Feb 2008 02:30:11 GMT</pubDate><guid isPermaLink="false">UPDATED RELEASE: 1.0 (Feb 20, 2008) 20080222A</guid></item><item><title>UPDATED RELEASE: 1.0 (Feb 20, 2008)</title><link>http://code.msdn.microsoft.com/MersenneTwister/Release/ProjectReleases.aspx?ReleaseId=439</link><description>First release of the class.</description><author></author><pubDate>Thu, 21 Feb 2008 13:12:15 GMT</pubDate><guid isPermaLink="false">UPDATED RELEASE: 1.0 (Feb 20, 2008) 20080221P</guid></item><item><title>UPDATED WIKI: Home</title><link>http://code.msdn.microsoft.com/MersenneTwister/Wiki/View.aspx?title=Home&amp;version=5</link><description>&lt;div class="wikidoc"&gt;
The Mersenne Twister is a rather well-known algorithm for generating pseudo-random numbers. It is fast and has a huge period &amp;#40;range over which it doesn&amp;#39;t repeat itself&amp;#41;, which makes it ideal for simulation. However, the sequence can be guessed after only 624 numbers have been generated, so it is not useful for cryptography. It was first published in 1998 by Makoto Matsumoto and Takuji Nishimura using C code. 
*&lt;br /&gt; &lt;br /&gt;An acquaintance of mine, Trevor Misfeldt, converted the C code to C# and published it to &lt;a href="http://www.c-sharpcorner.com/uploadfile/trevormisfeldt/mersennetwisteralgo08302005002422am/mersennetwisteralgo.aspx" class="externalLink"&gt;c-sharpcorner&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. Another implementation in C# is from Akihilo Kramot at &lt;a href="http://www.takel.jp/mt/MersenneTwister.cs" class="externalLink"&gt;http://www.takel.jp/mt/MersenneTwister.cs&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;.&lt;br /&gt; &lt;br /&gt;I've taken both of these and created this version, since Trevor's is a fairly straight conversion from the C-code, yet has some compelling methods, and Akihilo Kramot's implementation is clean and well-adapted to .Net (implements System.Random), but contains a small bug in the NextBytes method.&lt;br /&gt; &lt;br /&gt;I have also implemented the 53-bit variety of the algorithm for the NextDouble() method after an &lt;a href="http://www.springerlink.com/content/p5330n4932063068" class="externalLink"&gt;article &lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; was published in the journal &lt;i&gt;Monte Carlo Methods and Applications&lt;/i&gt;, Vol. 12, No. 5-6, pp. 385 – 393 (2006), entitled &amp;quot;A Repetition Test for Pseudo-Random Number Generators&amp;quot; highlighting that, somewhat unsurprisingly, the 32-bit variety for double floating points makes numbers repeat too soon in a sequence, whereas the 53-bit variety does not.&lt;br /&gt; &lt;br /&gt;This code is currently being used in the &lt;a href="http://www.codeplex.com/NPack" class="externalLink"&gt;NPack&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; library to provide random numbers for the numerical algorithms there. That project also has rudimentary tests for sanity checking (testing correct parameter guarding, primarily). I also hope to add at least one randomness test in order to verify the implementation (the algorithm has already tested).&lt;br /&gt; &lt;br /&gt;For the interested, more information on the Mersenne Twister can be found on &lt;a href="http://en.wikipedia.org/wiki/Mersenne_twister" class="externalLink"&gt;Wackypedia&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and appears, as of this posting, fairly accurate and useful. This could, however, change at any moment.&lt;br /&gt; &lt;br /&gt;* Published in the ACM's Transactions on Modeling and Computer Simulation Vol. 8, No. 1, January pp.3-30 (1998).&lt;br /&gt;
&lt;/div&gt;</description><author>codekaizen</author><pubDate>Thu, 21 Feb 2008 13:04:45 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: Home 20080221P</guid></item><item><title>CREATED RELEASE: 1.0 (Feb 20, 2008)</title><link>http://code.msdn.microsoft.com/MersenneTwister/Release/ProjectReleases.aspx?ReleaseId=439</link><description>First release of the class.</description><author></author><pubDate>Thu, 21 Feb 2008 04:18:05 GMT</pubDate><guid isPermaLink="false">CREATED RELEASE: 1.0 (Feb 20, 2008) 20080221A</guid></item><item><title>UPDATED WIKI: Home</title><link>http://code.msdn.microsoft.com/MersenneTwister/Wiki/View.aspx?title=Home&amp;version=4</link><description>&lt;div class="wikidoc"&gt;
The Mersenne Twister is a rather well-known algorithm for generating pseudo-random numbers. It is fast and has a huge period &amp;#40;range over which it doesn&amp;#39;t repeat itself&amp;#41;, which makes it ideal for simulation. However, the sequence can be guessed after only 624 numbers have been generated, so it is not useful for cryptography. It was first published in 1998 by Makoto Matsumoto and Takuji Nishimura using C code. 
*&lt;br /&gt; &lt;br /&gt;An acquaintance of mine, Trevor Misfeldt, converted the C code to C# and published it to &lt;a href="http://www.c-sharpcorner.com/uploadfile/trevormisfeldt/mersennetwisteralgo08302005002422am/mersennetwisteralgo.aspx" class="externalLink"&gt;c-sharpcorner&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. Another implementation in C# is from Akihilo Kramot at &lt;a href="http://www.takel.jp/mt/MersenneTwister.cs" class="externalLink"&gt;http://www.takel.jp/mt/MersenneTwister.cs&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;.&lt;br /&gt; &lt;br /&gt;I've taken both of these and created this version, since Trevor's is a fairly straight conversion from the C-code, yet has some compelling methods, and Akihilo Kramot's implementation is clean and well-adapted to .Net (implements System.Random), but contains a small bug in the NextBytes method.&lt;br /&gt; &lt;br /&gt;This code is currently being used in the &lt;a href="http://www.codeplex.com/NPack" class="externalLink"&gt;NPack&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; library to provide random numbers for the numerical algorithms there. That project also has rudimentary tests for sanity checking (testing correct parameter guarding, primarily). I also hope to add at least one randomness test in order to verify the implementation (the algorithm has already tested).&lt;br /&gt; &lt;br /&gt;For the interested, more information on the Mersenne Twister can be found on &lt;a href="http://en.wikipedia.org/wiki/Mersenne_twister" class="externalLink"&gt;Wackypedia&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and appears, as of this posting, fairly accurate and useful. This could, however, change at any moment.&lt;br /&gt; &lt;br /&gt;* Published in the ACM's Transactions on Modeling and Computer Simulation Vol. 8, No. 1, January pp.3-30 (1998).&lt;br /&gt;
&lt;/div&gt;</description><author>codekaizen</author><pubDate>Thu, 21 Feb 2008 04:15:17 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: Home 20080221A</guid></item><item><title>UPDATED WIKI: Home</title><link>http://code.msdn.microsoft.com/MersenneTwister/Wiki/View.aspx?title=Home&amp;version=3</link><description>&lt;div class="wikidoc"&gt;
The Mersenne Twister is a rather well-known algorithm for generating pseudo-random numbers. It is fast and has a huge period &amp;#40;range over which it doesn&amp;#39;t repeat itself&amp;#41;, which makes it ideal for simulation. However, the sequence can be guessed after only 624 numbers have been generated, so it is not useful for cryptography. It was first published in 1998 by Makoto Matsumoto and Takuji Nishimura using C code. 
*&lt;br /&gt; &lt;br /&gt;An acquaintance of mine, Trevor Misfeldt, converted the C code to C# and published it to &lt;a href="http://www.c-sharpcorner.com/uploadfile/trevormisfeldt/mersennetwisteralgo08302005002422am/mersennetwisteralgo.aspx" class="externalLink"&gt;c-sharpcorner&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. Another implementation in C# is from Akihilo Kramot at &lt;a href="http://www.takel.jp/mt/MersenneTwister.cs" class="externalLink"&gt;http://www.takel.jp/mt/MersenneTwister.cs&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;.&lt;br /&gt; &lt;br /&gt;I've taken both of these and created this version, since Trevor's is a very straight conversion from the C-code, yet has some compelling methods, and Akihilo Kramot's implementation is clean and well-adapted to .Net (implements System.Random), but contains a small bug in the NextBytes method.&lt;br /&gt; &lt;br /&gt;I also hope to add at least one randomness test in order to verify the implementation (the algorithm has already tested).&lt;br /&gt; &lt;br /&gt;For the interested, more information on the Mersenne Twister can be found on &lt;a href="http://en.wikipedia.org/wiki/Mersenne_twister" class="externalLink"&gt;Wackypedia&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and appears, as of this posting, fairly accurate and useful. This could, however, change at any moment.&lt;br /&gt; &lt;br /&gt;* Published in the ACM's Transactions on Modeling and Computer Simulation Vol. 8, No. 1, January pp.3-30 (1998).&lt;br /&gt;
&lt;/div&gt;</description><author>codekaizen</author><pubDate>Thu, 21 Feb 2008 04:00:08 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: Home 20080221A</guid></item><item><title>UPDATED WIKI: Home</title><link>http://code.msdn.microsoft.com/MersenneTwister/Wiki/View.aspx?title=Home&amp;version=2</link><description>&lt;div class="wikidoc"&gt;
The Mersenne Twister is a rather well-known algorithm for generating pseudo-random numbers. It was first published in 1998 by Makoto Matsumoto and Takuji Nishimura using C code. 
*&lt;br /&gt; &lt;br /&gt;A friend of mine, Trevor Misfeldt, converted the C code to C# and published it to &lt;a href="http://www.c-sharpcorner.com/uploadfile/trevormisfeldt/mersennetwisteralgo08302005002422am/mersennetwisteralgo.aspx" class="externalLink"&gt;c-sharpcorner&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;. Another implementation in C# is from Akihilo Kramot at &lt;a href="http://www.takel.jp/mt/MersenneTwister.cs" class="externalLink"&gt;http://www.takel.jp/mt/MersenneTwister.cs&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt;.&lt;br /&gt; &lt;br /&gt;I've taken both of these and created this version, since Trevor's is a very straight conversion from the C-code, yet has some compelling methods, and Akihilo Kramot's implementation is clean and well-adapted to .Net (implements System.Random), but contains a bug in the NextBytes method.&lt;br /&gt; &lt;br /&gt;I also hope to add at least one randomness test &lt;br /&gt; &lt;br /&gt;For the interested, more information on the Mersenne Twister can be found on &lt;a href="http://en.wikipedia.org/wiki/Mersenne_twister" class="externalLink"&gt;Wackypedia&lt;span class="externalLinkIcon"&gt;&lt;/span&gt;&lt;/a&gt; and appears, as of this posting, fairly accurate and useful. This could, however, change at any moment.&lt;br /&gt; &lt;br /&gt;* Published in the ACM's Transactions on Modeling and Computer Simulation Vol. 8, No. 1, January pp.3-30 (1998).&lt;br /&gt;
&lt;/div&gt;</description><author>codekaizen</author><pubDate>Thu, 21 Feb 2008 03:22:13 GMT</pubDate><guid isPermaLink="false">UPDATED WIKI: Home 20080221A</guid></item></channel></rss>