﻿<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Steve Evans</title>
    <description>Random tips, facts, and thoughts by Steve Evans, a.k.a. Sleepless Monkey, as he continues to grow as a developer and consultant.</description>
    <link>http://www.maddotnet.com/NewsBlogsJobs/tabid/150/BlogId/33/Default.aspx</link>
    <language>en-US</language>
    <managingEditor>sevans@sleeplessmonkey.com</managingEditor>
    <webMaster>mail@lancelarsen.com</webMaster>
    <pubDate>Fri, 12 Mar 2010 02:26:08 GMT</pubDate>
    <lastBuildDate>Fri, 12 Mar 2010 02:26:08 GMT</lastBuildDate>
    <docs>http://backend.userland.com/rss</docs>
    <generator>Blog RSS Generator Version 3.5.0.35082</generator>
    <item>
      <title>Speeding up podcasts within iTunes</title>
      <description>&lt;p&gt;&lt;p&gt;What started out as a simple &lt;a href="http://superuser.com/questions/81045/podcast-playback-faster-than-1x-speed-on-ipod"&gt;Superuser question&lt;/a&gt; became a long trek to find a solution.  The only &lt;a href="http://superuser.com/questions/81045/podcast-playback-faster-than-1x-speed-on-ipod/85584#85584"&gt;answer&lt;/a&gt; at the time pointed me to do batch processing through &lt;a href="http://audacity.sourceforge.net/"&gt;Audacity&lt;/a&gt;, but I was trying to avoid automating the GUI as much as possible.  Although I know how to make use of &lt;span&gt;...&lt;/span&gt;&lt;/p&gt;&lt;/p&gt;&lt;b&gt;Note:&lt;/b&gt; Cross posted from &lt;a href="http://sleeplessmonkey.blogspot.com/"&gt;Sleepless Monkey's Adventures&lt;/a&gt;.
&lt;br /&gt;&lt;a href="http://sleeplessmonkey.blogspot.com/2010/01/speeding-up-podcasts-within-itunes.html"&gt;Permalink&lt;/a&gt;
&lt;br /&gt;</description>
      <author>sevans@sleeplessmonkey.com</author>
      <guid isPermaLink="true">http://www.maddotnet.com/NewsBlogsJobs/tabid/150/EntryId/219/Speeding-up-podcasts-within-iTunes.aspx</guid>
      <pubDate>Thu, 07 Jan 2010 21:32:32 GMT</pubDate>
      <trackback:ping>http://www.maddotnet.com/DesktopModules/Blog/Trackback.aspx?id=219</trackback:ping>
    </item>
    <item>
      <title>Chicago Code Camp Retrospective</title>
      <description>&lt;p&gt;&lt;p&gt;Today was a long day, because I made the trek down to the Chicago Code Camp.  Aside from the two hour drive each way, it was a good experience.  Here's a summary of the sessions.&lt;/p&gt;  &lt;h4&gt;Trends in Continuous Integration with Software Delivery&lt;span&gt;...&lt;/span&gt;&lt;/h4&gt;&lt;/p&gt;&lt;b&gt;Note:&lt;/b&gt; Cross posted from &lt;a href="http://sleeplessmonkey.blogspot.com/index.html"&gt;Sleepless Monkey's Adventures&lt;/a&gt;.
&lt;br /&gt;&lt;a href="http://sleeplessmonkey.blogspot.com/2009/05/chicago-code-camp-retrospective.html"&gt;Permalink&lt;/a&gt;
&lt;br /&gt;</description>
      <link>http://maddotnet.com/NewsBlogsJobs/tabid/150/EntryId/147/Chicago-Code-Camp-Retrospective.aspx</link>
      <author>sevans@sleeplessmonkey.com</author>
      <guid isPermaLink="true">http://maddotnet.com/NewsBlogsJobs/tabid/150/EntryId/147/Chicago-Code-Camp-Retrospective.aspx</guid>
      <pubDate>Sun, 31 May 2009 14:31:23 GMT</pubDate>
      <trackback:ping>http://www.maddotnet.com/DesktopModules/Blog/Trackback.aspx?id=147</trackback:ping>
    </item>
    <item>
      <title>Chicago Code Camp Retrospective</title>
      <description>&lt;p&gt;&lt;p&gt;Today was a long day, because I made the trek down to the Chicago Code Camp.  Aside from the two hour drive each way, it was a good experience.  Here's a summary of the sessions.&lt;/p&gt;  &lt;h4&gt;Trends in Continuous Integration with Software Delivery&lt;span&gt;...&lt;/span&gt;&lt;/h4&gt;&lt;/p&gt;&lt;b&gt;Note:&lt;/b&gt; Cross posted from &lt;a href="http://sleeplessmonkey.blogspot.com/index.html"&gt;Sleepless Monkey's Adventures&lt;/a&gt;.
&lt;br /&gt;&lt;a href="http://sleeplessmonkey.blogspot.com/2009/05/chicago-code-camp-retrospective.html"&gt;Permalink&lt;/a&gt;
&lt;br /&gt;</description>
      <link>http://maddotnet.com/Home/tabid/131/EntryId/193/Chicago-Code-Camp-Retrospective.aspx</link>
      <category domain="http://www.maddotnet.com/newsblogsjobs/tabid/150/blogid/42/default.aspx">Steve Evans</category>
      <author>sevans@sleeplessmonkey.com</author>
      <guid isPermaLink="true">http://maddotnet.com/Home/tabid/131/EntryId/193/Chicago-Code-Camp-Retrospective.aspx</guid>
      <pubDate>Sun, 31 May 2009 14:31:23 GMT</pubDate>
      <trackback:ping>http://www.maddotnet.com/DesktopModules/Blog/Trackback.aspx?id=193</trackback:ping>
    </item>
    <item>
      <title>The difference in random number generation in .NET</title>
      <description>&lt;p&gt;The next project I plan on working on is going to rely heavily on a &lt;a href="http://en.wikipedia.org/wiki/Monte_Carlo_method" target="_blank"&gt;Monte Carlo method&lt;/a&gt; of populating the initial state.  Knowing this, I started digging into the different ways of generating random numbers in .NET.  Everybody immediately goes with &lt;a href="http://msdn.microsoft.com/en-us/library/system.random.aspx" target="_blank"&gt;System.Random&lt;/a&gt; when they're starting out since it's readily visible in a new class file.  This is all well and good, but you run into the issue that what it gives you is a pseudo-random number.  It's actually pretty interesting to pop open Reflector and look at what it's doing under the hood.  Granted it uses the system's ticks to seed the random number if you don't provide one yourself, but it does mean that processes (or even threads) creating an instance in the same tick will have the same random numbers.  Well that can be a problem...&lt;/p&gt; &lt;p&gt;So how do we get a more *random* random number? That's where &lt;a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.rngcryptoserviceprovider.aspx" target="_blank"&gt;System.Security.Cryptography.RNGCryptoServiceProvider&lt;/a&gt; comes into the picture.  Digging into this class it's making an external call, so not quite as easy to figure out how it's getting it's randomness.  It really doesn't matter too much because it does come up with a better randomness.  The drawbacks of using it though are that the way to work with it is quite a bit different than the System.Random class, and it is quite a bit slower.  With the RNGCryptoServiceProvider you have to work with a byte array and do the conversions to whatever random type you want to work with.  So if you build out your classes to use System.Random and later want to switch to RNGCryptoServiceProvider you have your work cut out for you.  Or what about using RNGCryptoServiceProvider in production for "better" randomness, but use System.Random when you're unit/integration testing because you can control it?  There isn't a common interface to use in that case, but I've been tinkering around getting such an interface set most of the day.  Once I have it fleshed out more I'll post it up here.&lt;/p&gt; &lt;p&gt;Now back to the slowness of RNGCryptoServiceProvider vs. Random.  Finding a few useful answers on Stack Overflow there was mention of it being slower, but never really gave any indication of how much worse it was.  I've seen too many statements like this and the difference really only came down to maybe a few hundred milliseconds (so not a noticeable problem in the applications I've worked on).  So I decided to toss together a test and find out just how much slower it is.  Here's what I did to test it:&lt;/p&gt; &lt;div class="csharpcode"&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; ConsoleApplication1&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;using&lt;/span&gt; System;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;using&lt;/span&gt; System.Diagnostics;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    &lt;span class="kwrd"&gt;using&lt;/span&gt; System.Security.Cryptography;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt; &lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    &lt;span class="kwrd"&gt;class&lt;/span&gt; Program&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;    {&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;        &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;        {&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;            Stopwatch stopwatch = &lt;span class="kwrd"&gt;new&lt;/span&gt; Stopwatch();&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;            RNGCryptoServiceProvider gen = &lt;span class="kwrd"&gt;new&lt;/span&gt; RNGCryptoServiceProvider();&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;            Random generator = &lt;span class="kwrd"&gt;new&lt;/span&gt; Random();&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;            &lt;span class="rem"&gt;//Since I'm only working with int for both randomizers...&lt;/span&gt;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;            &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] randomValues = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;byte&lt;/span&gt;[ &lt;span class="kwrd"&gt;sizeof&lt;/span&gt;(Int32) ]; &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt; &lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;            &lt;span class="rem"&gt;//step up the iterations by from 1 to 100,000,000; advancing by power of 10&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;            &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; iterations = 1; iterations &lt;= Math.Pow(10,8); iterations *= 10)&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;            {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;                Console.WriteLine(String.Format(&lt;span class="str"&gt;"Iterations: {0}"&lt;/span&gt;, iterations));&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;                &lt;span class="rem"&gt;//Start the RNGCryptoServiceProvider timing&lt;/span&gt;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;                stopwatch.Reset();&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;                stopwatch.Start();&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;                &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &lt; iterations; i++)&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;                {&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt;                    gen.GetBytes(randomValues);&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  28:  &lt;/span&gt;                    BitConverter.ToInt32(randomValues, 0);&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  29:  &lt;/span&gt;                }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  30:  &lt;/span&gt;                stopwatch.Stop();&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  31:  &lt;/span&gt;                TimeSpan rngRandom = stopwatch.Elapsed;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  32:  &lt;/span&gt;                Console.WriteLine(&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  33:  &lt;/span&gt;                    String.Format(&lt;span class="str"&gt;"\tRNGCryptoServiceProvider:\t{0}"&lt;/span&gt;, stopwatch.Elapsed));&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  34:  &lt;/span&gt; &lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  35:  &lt;/span&gt;                &lt;span class="rem"&gt;//Start the System.Random timing&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  36:  &lt;/span&gt;                stopwatch.Reset();&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  37:  &lt;/span&gt;                stopwatch.Start();&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  38:  &lt;/span&gt;                &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &lt; iterations; i++)&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  39:  &lt;/span&gt;                {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  40:  &lt;/span&gt;                    generator.Next();&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  41:  &lt;/span&gt;                }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  42:  &lt;/span&gt;                stopwatch.Stop();&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  43:  &lt;/span&gt;                TimeSpan sysRandom = stopwatch.Elapsed;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  44:  &lt;/span&gt;                Double speedFactor = Convert.ToDouble(rngRandom.Ticks) / Convert.ToDouble(sysRandom.Ticks);&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  45:  &lt;/span&gt;                Console.WriteLine(&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  46:  &lt;/span&gt;                    String.Format(&lt;span class="str"&gt;"\tSystem.Random:           \t{0}\t~{1:0.00}x faster"&lt;/span&gt;,&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  47:  &lt;/span&gt;                                    stopwatch.Elapsed,&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  48:  &lt;/span&gt;                                    speedFactor));&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  49:  &lt;/span&gt;                Console.WriteLine();&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  50:  &lt;/span&gt;            }&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  51:  &lt;/span&gt;            Console.ReadLine();&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  52:  &lt;/span&gt;        }&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  53:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  54:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;And here's the results:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://i592.photobucket.com/albums/tt2/Agent_9191/RandomNumberTimes.png" target="_blank"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="267" alt="image" src="/Portals/www_maddotnet_com/Blog/Files/33/139/WLW-AGenericRandomGeneratorinC_117E0-image_7.png" width="409" border="0"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The times varied a bit on my Core 2 Duo  2.50 GHz, 32-bit Vista laptop, but this isn't too far from the norm.  It wasn't until 100,000 iterations that it took more that 1/10 of a second to finish, but at that point System.Random was ~279.2 times faster!  The big thing is that even though the number of iterations are was growing by a factor of 10, RNGCryptoServiceProvider seemed to increase the time by more than a factor of 10.&lt;/p&gt;
&lt;p&gt;So I guess the bottom line in the System.Random vs. RNGCryptoServiceProvider argument over slowness is in small numbers it doesn't greatly matter.  Now if you're going to be generating more than 100,000 numbers in a very tight loop, it might be worth sacrificing "true" randomness with speed.&lt;/p&gt;</description>
      <link>http://maddotnet.com/NewsBlogsJobs/tabid/150/EntryId/139/The-difference-in-random-number-generation-in-NET.aspx</link>
      <author>sevans@sleeplessmonkey.com</author>
      <comments>http://maddotnet.com/NewsBlogsJobs/tabid/150/EntryId/139/The-difference-in-random-number-generation-in-NET.aspx#Comments</comments>
      <guid isPermaLink="true">http://maddotnet.com/NewsBlogsJobs/tabid/150/EntryId/139/The-difference-in-random-number-generation-in-NET.aspx</guid>
      <pubDate>Fri, 13 Mar 2009 03:31:54 GMT</pubDate>
      <slash:comments>1</slash:comments>
      <trackback:ping>http://www.maddotnet.com/DesktopModules/Blog/Trackback.aspx?id=139</trackback:ping>
    </item>
    <item>
      <title>The difference in random number generation in .NET</title>
      <description>&lt;p&gt;The next project I plan on working on is going to rely heavily on a &lt;a href="http://en.wikipedia.org/wiki/Monte_Carlo_method" target="_blank"&gt;Monte Carlo method&lt;/a&gt; of populating the initial state.  Knowing this, I started digging into the different ways of generating random numbers in .NET.  Everybody immediately goes with &lt;a href="http://msdn.microsoft.com/en-us/library/system.random.aspx" target="_blank"&gt;System.Random&lt;/a&gt; when they're starting out since it's readily visible in a new class file.  This is all well and good, but you run into the issue that what it gives you is a pseudo-random number.  It's actually pretty interesting to pop open Reflector and look at what it's doing under the hood.  Granted it uses the system's ticks to seed the random number if you don't provide one yourself, but it does mean that processes (or even threads) creating an instance in the same tick will have the same random numbers.  Well that can be a problem...&lt;/p&gt; &lt;p&gt;So how do we get a more *random* random number? That's where &lt;a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.rngcryptoserviceprovider.aspx" target="_blank"&gt;System.Security.Cryptography.RNGCryptoServiceProvider&lt;/a&gt; comes into the picture.  Digging into this class it's making an external call, so not quite as easy to figure out how it's getting it's randomness.  It really doesn't matter too much because it does come up with a better randomness.  The drawbacks of using it though are that the way to work with it is quite a bit different than the System.Random class, and it is quite a bit slower.  With the RNGCryptoServiceProvider you have to work with a byte array and do the conversions to whatever random type you want to work with.  So if you build out your classes to use System.Random and later want to switch to RNGCryptoServiceProvider you have your work cut out for you.  Or what about using RNGCryptoServiceProvider in production for "better" randomness, but use System.Random when you're unit/integration testing because you can control it?  There isn't a common interface to use in that case, but I've been tinkering around getting such an interface set most of the day.  Once I have it fleshed out more I'll post it up here.&lt;/p&gt; &lt;p&gt;Now back to the slowness of RNGCryptoServiceProvider vs. Random.  Finding a few useful answers on Stack Overflow there was mention of it being slower, but never really gave any indication of how much worse it was.  I've seen too many statements like this and the difference really only came down to maybe a few hundred milliseconds (so not a noticeable problem in the applications I've worked on).  So I decided to toss together a test and find out just how much slower it is.  Here's what I did to test it:&lt;/p&gt; &lt;div class="csharpcode"&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; ConsoleApplication1&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;using&lt;/span&gt; System;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;using&lt;/span&gt; System.Diagnostics;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    &lt;span class="kwrd"&gt;using&lt;/span&gt; System.Security.Cryptography;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt; &lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    &lt;span class="kwrd"&gt;class&lt;/span&gt; Program&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;    {&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;        &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;        {&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;            Stopwatch stopwatch = &lt;span class="kwrd"&gt;new&lt;/span&gt; Stopwatch();&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;            RNGCryptoServiceProvider gen = &lt;span class="kwrd"&gt;new&lt;/span&gt; RNGCryptoServiceProvider();&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;            Random generator = &lt;span class="kwrd"&gt;new&lt;/span&gt; Random();&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;            &lt;span class="rem"&gt;//Since I'm only working with int for both randomizers...&lt;/span&gt;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;            &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] randomValues = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;byte&lt;/span&gt;[ &lt;span class="kwrd"&gt;sizeof&lt;/span&gt;(Int32) ]; &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt; &lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;            &lt;span class="rem"&gt;//step up the iterations by from 1 to 100,000,000; advancing by power of 10&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;            &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; iterations = 1; iterations &lt;= Math.Pow(10,8); iterations *= 10)&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;            {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;                Console.WriteLine(String.Format(&lt;span class="str"&gt;"Iterations: {0}"&lt;/span&gt;, iterations));&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;                &lt;span class="rem"&gt;//Start the RNGCryptoServiceProvider timing&lt;/span&gt;&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;                stopwatch.Reset();&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;                stopwatch.Start();&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;                &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &lt; iterations; i++)&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;                {&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt;                    gen.GetBytes(randomValues);&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  28:  &lt;/span&gt;                    BitConverter.ToInt32(randomValues, 0);&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  29:  &lt;/span&gt;                }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  30:  &lt;/span&gt;                stopwatch.Stop();&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  31:  &lt;/span&gt;                TimeSpan rngRandom = stopwatch.Elapsed;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  32:  &lt;/span&gt;                Console.WriteLine(&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  33:  &lt;/span&gt;                    String.Format(&lt;span class="str"&gt;"\tRNGCryptoServiceProvider:\t{0}"&lt;/span&gt;, stopwatch.Elapsed));&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  34:  &lt;/span&gt; &lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  35:  &lt;/span&gt;                &lt;span class="rem"&gt;//Start the System.Random timing&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  36:  &lt;/span&gt;                stopwatch.Reset();&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  37:  &lt;/span&gt;                stopwatch.Start();&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  38:  &lt;/span&gt;                &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &lt; iterations; i++)&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  39:  &lt;/span&gt;                {&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  40:  &lt;/span&gt;                    generator.Next();&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  41:  &lt;/span&gt;                }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  42:  &lt;/span&gt;                stopwatch.Stop();&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  43:  &lt;/span&gt;                TimeSpan sysRandom = stopwatch.Elapsed;&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  44:  &lt;/span&gt;                Double speedFactor = Convert.ToDouble(rngRandom.Ticks) / Convert.ToDouble(sysRandom.Ticks);&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  45:  &lt;/span&gt;                Console.WriteLine(&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  46:  &lt;/span&gt;                    String.Format(&lt;span class="str"&gt;"\tSystem.Random:           \t{0}\t~{1:0.00}x faster"&lt;/span&gt;,&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  47:  &lt;/span&gt;                                    stopwatch.Elapsed,&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  48:  &lt;/span&gt;                                    speedFactor));&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  49:  &lt;/span&gt;                Console.WriteLine();&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  50:  &lt;/span&gt;            }&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  51:  &lt;/span&gt;            Console.ReadLine();&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  52:  &lt;/span&gt;        }&lt;/pre&gt;&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  53:  &lt;/span&gt;    }&lt;/pre&gt;&lt;pre&gt;&lt;span class="lnum"&gt;  54:  &lt;/span&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;And here's the results:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://i592.photobucket.com/albums/tt2/Agent_9191/RandomNumberTimes.png" target="_blank"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="267" alt="image" src="/Portals/www_maddotnet_com/Blog/Files/33/139/WLW-AGenericRandomGeneratorinC_117E0-image_7.png" width="409" border="0"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The times varied a bit on my Core 2 Duo  2.50 GHz, 32-bit Vista laptop, but this isn't too far from the norm.  It wasn't until 100,000 iterations that it took more that 1/10 of a second to finish, but at that point System.Random was ~279.2 times faster!  The big thing is that even though the number of iterations are was growing by a factor of 10, RNGCryptoServiceProvider seemed to increase the time by more than a factor of 10.&lt;/p&gt;
&lt;p&gt;So I guess the bottom line in the System.Random vs. RNGCryptoServiceProvider argument over slowness is in small numbers it doesn't greatly matter.  Now if you're going to be generating more than 100,000 numbers in a very tight loop, it might be worth sacrificing "true" randomness with speed.&lt;/p&gt;</description>
      <link>http://maddotnet.com/Home/tabid/131/EntryId/194/The-difference-in-random-number-generation-in-NET.aspx</link>
      <category domain="http://www.maddotnet.com/newsblogsjobs/tabid/150/blogid/42/default.aspx">Steve Evans</category>
      <author>sevans@sleeplessmonkey.com</author>
      <comments>http://maddotnet.com/Home/tabid/131/EntryId/194/The-difference-in-random-number-generation-in-NET.aspx#Comments</comments>
      <guid isPermaLink="true">http://maddotnet.com/Home/tabid/131/EntryId/194/The-difference-in-random-number-generation-in-NET.aspx</guid>
      <pubDate>Fri, 13 Mar 2009 03:31:54 GMT</pubDate>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.maddotnet.com/DesktopModules/Blog/Trackback.aspx?id=194</trackback:ping>
    </item>
  </channel>
</rss>