title: Need a deterministic algorithm to generate a resource domain prefix for a given path in an evenly distributed fashion
body: <p>I need to generate a resource domain prefix based on a given path and configured number of resource domains in a deterministic fashion with good distribution. For example, if you pass it the path "/script/site.js" it returns "res#", where '#' is an integer between 0 and the configured amount of resource domains.</p>
  
  <p>Using C# 3.0.</p>
  
  <p>So far have this:</p>
  
  <pre><code>var resourceDomainCount = 4;
  var hasher = System.Security.Cryptography.SHA1.Create();
  var paths = new [] {
      "/App_Themes.0.1.433.232/images/buttonsBackgrounds.jpg",
      "/App_Themes.0.1.433.232/images/blahblah.jpg",
      "/App_Themes.0.1.433.232/images/pagebg.gif",
      "/App_Themes.0.1.433.232/site.css",
      "/script/site.js",
      "/App_Themes.0.1.433.232/images/different.jpg",
      "/App_Themes.0.1.433.232/images/shadows.jpg",
      "/Handlers/ImageHandler.ashx?type=l&amp;id=123&amp;s=g&amp;index=0",
      "/Handlers/ImageHandler.ashx?type=l&amp;id=234&amp;s=g&amp;index=0",
      "/Handlers/ImageHandler.ashx?type=l&amp;id=345&amp;s=g&amp;index=0",
      "/Handlers/ImageHandler.ashx?type=p&amp;id=MyProduct&amp;s=g&amp;index=0",
      "/Handlers/ImageHandler.ashx?type=p&amp;id=WineGreat&amp;s=g&amp;index=0",
      "/Handlers/ImageHandler.ashx?type=p&amp;id=YayYay&amp;s=g&amp;index=0"
      };
  foreach (var path in paths) {
      var pathHash = hasher.ComputeHash(Encoding.ASCII.GetBytes(path));
      var singleByteHash = pathHash.Aggregate(0, (a, b) =&gt; a ^ b);
      var random = new Random((int)singleByteHash);
      var resourceDomainIndex = random.Next(0, resourceDomainCount);
      (resourceDomainIndex + ": " + path).Dump();
  }
  </code></pre>
  
  <p>Which produces the following:</p>
  
  <p>3: /App_Themes.0.1.433.232/images/buttonsBackgrounds.jpg<br>
  0: /App_Themes.0.1.433.232/images/blahblah.jpg<br>
  1: /App_Themes.0.1.433.232/images/pagebg.gif<br>
  1: /App_Themes.0.1.433.232/site.css<br>
  3: /script/site.js<br>
  1: /App_Themes.0.1.433.232/images/different.jpg<br>
  3: /App_Themes.0.1.433.232/images/shadows.jpg<br>
  1: /Handlers/ImageHandler.ashx?type=l&amp;id=123&amp;s=g&amp;index=0<br>
  1: /Handlers/ImageHandler.ashx?type=l&amp;id=234&amp;s=g&amp;index=0<br>
  0: /Handlers/ImageHandler.ashx?type=l&amp;id=345&amp;s=g&amp;index=0<br>
  2: /Handlers/ImageHandler.ashx?type=p&amp;id=MyProduct&amp;s=g&amp;index=0<br>
  1: /Handlers/ImageHandler.ashx?type=p&amp;id=WineGreat&amp;s=g&amp;index=0<br>
  0: /Handlers/ImageHandler.ashx?type=p&amp;id=YayYay&amp;s=g&amp;index=0  </p>
  
  <p>Not getting the distribution I want (there is only one instance of '2' there).</p>
  
  <p>There are thousands of input strings that change all the time and the paths will be generated at runtime, e.g.: <code>&lt;a href="&lt;%= GetPath("/script/site.js") %&gt;"&gt;Link&lt;/a&gt;</code></p>