<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>A Simple Puzzle</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/02/24/a-simple-puzzle.aspx</link><description>My original version of the histogram-generating code that I whipped up for the previous episode of FAIC contained a subtle bug. Can you spot it without going back and reading the corrected code? private static int[] CreateHistogram(IEnumerable&amp;lt;double&amp;gt;</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: A Simple Puzzle</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/02/24/a-simple-puzzle.aspx#10296156</link><pubDate>Sat, 21 Apr 2012 23:07:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10296156</guid><dc:creator>pr3fix</dc:creator><description>&lt;p&gt;(max - min) / buckets ?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10296156" width="1" height="1"&gt;</description></item><item><title>re: A Simple Puzzle</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/02/24/a-simple-puzzle.aspx#10274288</link><pubDate>Wed, 29 Feb 2012 10:38:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10274288</guid><dc:creator>CodeInChaos</dc:creator><description>&lt;p&gt;@Ali&lt;/p&gt;
&lt;p&gt;The disadvantage of that method is, that you now need to be *very* careful about rounding errors. I would not be surprised if there are values min&amp;lt;=x&amp;lt;max for which the calculated bucket is outside of the array range. Floating point rounding is a tricky beast.&lt;/p&gt;
&lt;p&gt;So I prefer Eric&amp;#39;s code (with a fix for his bug of course).&lt;/p&gt;
&lt;p&gt;---&lt;/p&gt;
&lt;p&gt;Since IMO the build in Random class in .net sucks, I&amp;#39;m currently creating my own RNG. A similar issue crept up:&lt;/p&gt;
&lt;p&gt;One feature is generating a uniform double that satisfies min&amp;lt;=x&amp;lt;max from a uniform double that satisfies 0&amp;lt;=x&amp;lt;1.&lt;/p&gt;
&lt;p&gt;The naive implementation if this is simply: `return min+(max-min)*Uniform()`. Unfortunately if `Uniform()` returns `1-Epsilon`, the result might get rounded to `max`, violating the contract.&lt;/p&gt;
&lt;p&gt;So I&amp;#39;ve added a do-while loop ensured the result is smaller than max.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10274288" width="1" height="1"&gt;</description></item><item><title>re: A Simple Puzzle</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/02/24/a-simple-puzzle.aspx#10273343</link><pubDate>Mon, 27 Feb 2012 09:12:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10273343</guid><dc:creator>Olivier</dc:creator><description>&lt;p&gt;The moral of all this: static typing does not replace testing ;-)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10273343" width="1" height="1"&gt;</description></item><item><title>re: A Simple Puzzle</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/02/24/a-simple-puzzle.aspx#10273280</link><pubDate>Mon, 27 Feb 2012 05:08:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10273280</guid><dc:creator>Jagannath</dc:creator><description>&lt;p&gt;private static int[] CreateHistogram(IEnumerable&amp;lt;double&amp;gt; data, int buckets, double min, double max)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;int[] results = new int[buckets];&lt;/p&gt;
&lt;p&gt; &amp;nbsp;double multiplier = buckets / (max - min);&lt;/p&gt;
&lt;p&gt; &amp;nbsp;foreach (double datum in data)&lt;/p&gt;
&lt;p&gt; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;int index = (int) ((datum - min) * multiplier);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;if (0 &amp;lt;= index &amp;amp;&amp;amp; index &amp;lt; buckets)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;results[index] += 1;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;min = datum;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp;return results;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10273280" width="1" height="1"&gt;</description></item><item><title>re: A Simple Puzzle</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/02/24/a-simple-puzzle.aspx#10272600</link><pubDate>Fri, 24 Feb 2012 19:39:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10272600</guid><dc:creator>weeble0</dc:creator><description>&lt;p&gt;@Ali Ah, sorry, I did misunderstand you.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10272600" width="1" height="1"&gt;</description></item><item><title>re: A Simple Puzzle</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/02/24/a-simple-puzzle.aspx#10272575</link><pubDate>Fri, 24 Feb 2012 19:04:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10272575</guid><dc:creator>Ali</dc:creator><description>&lt;p&gt;@weeble0 I think you misunderstand me, my comment was &amp;quot;first check if the current point is in range, and if it is, only then calculate the index&amp;quot; seems a more natural way to code the algorithm. I wondered if there is some disadvantage I cannot see. (Except that, then there would be no interesting bug/puzzle to talk about) &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10272575" width="1" height="1"&gt;</description></item><item><title>re: A Simple Puzzle</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/02/24/a-simple-puzzle.aspx#10272568</link><pubDate>Fri, 24 Feb 2012 18:55:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10272568</guid><dc:creator>weeble0</dc:creator><description>&lt;p&gt;This all sounds very familiar. This bears some relation to one of Eric&amp;#39;s previous posts, &amp;quot;What&amp;#39;s the difference? Remainder vs modulus&amp;quot;:&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/b/ericlippert/archive/2011/12/05/what-s-the-difference-remainder-vs-modulus.aspx?PageIndex=2"&gt;blogs.msdn.com/.../what-s-the-difference-remainder-vs-modulus.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In fact, I think in the comments I linked to an instance of a very similar bug.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10272568" width="1" height="1"&gt;</description></item><item><title>re: A Simple Puzzle</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/02/24/a-simple-puzzle.aspx#10272561</link><pubDate>Fri, 24 Feb 2012 18:45:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10272561</guid><dc:creator>weeble0</dc:creator><description>&lt;p&gt;@Ali I don&amp;#39;t think that has anything to do with the bug, does it? Mathematicians would normally write something like (min&amp;lt;=value&amp;lt;max) to more clearly express how value falls inside a range with a lower and an upper bound. Indeed some languages such as Python actually allow that syntax. In languages like C# that don&amp;#39;t allow that syntax, (min&amp;lt;=datum &amp;amp;&amp;amp; datum&amp;lt;max) is the closest you can get.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10272561" width="1" height="1"&gt;</description></item><item><title>re: A Simple Puzzle</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/02/24/a-simple-puzzle.aspx#10272560</link><pubDate>Fri, 24 Feb 2012 18:41:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10272560</guid><dc:creator>Josh Starner</dc:creator><description>&lt;p&gt;I just watched a presentation by Bret Victor last night called Inventing on Principle, and he talks about an interface that gives algorithm developers immediate insight into the results of their functions. &amp;nbsp;If you haven&amp;#39;t seen it, you should watch this section of it: &lt;a rel="nofollow" target="_new" href="http://www.youtube.com/watch?v=PUv66718DII&amp;amp;feature=player_detailpage#t=1007s"&gt;www.youtube.com/watch&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Watching the video and then reading this post made me think: Man, if Eric had this tool, he may not have had that bug. :)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10272560" width="1" height="1"&gt;</description></item><item><title>re: A Simple Puzzle</title><link>http://blogs.msdn.com/b/ericlippert/archive/2012/02/24/a-simple-puzzle.aspx#10272551</link><pubDate>Fri, 24 Feb 2012 18:29:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10272551</guid><dc:creator>bjorg</dc:creator><description>&lt;p&gt;The only time it goes into the last bucket is when the value is equal to max. &amp;nbsp;Did you mean to use a rounding function when going from double to int instead?&lt;/p&gt;
&lt;div class="yellowbox"&gt;
&lt;p&gt;I&amp;#39;m not following your train of thought here. Suppose min is 0, max is 5 and buckets is 10. Multiplier is 2. If one of the data is, say, 4.7 then we multiply by 2 to get 9.4 and then round off to 9, which is the index of the last bucket. But 4.7 is not equal to 5. So I&amp;#39;ve provided a counterexample to your claim; would you care to clarify the claim? -- Eric&lt;/p&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10272551" width="1" height="1"&gt;</description></item></channel></rss>