Live data from Hacker News

A common bug in published code

google.com

21–30 of 79 posts

Re: A common bug in published code

#21
post #5
post #3

The first few examples are fine. It's the unscaled ones later on, like "longarr[i] = (int) Math.random();" and especially "[Math.abs((int) Math.random()) % 3];" where the authors didn't notice that random() "returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0." Here's another extreme example: (int) Math.random() / Integer.MAX_VALUE % (maxScoreCount + 1); I eyeballed that about…

They are all in error. "(int)Math.random()" always returns 0 because Math.random() returns [0.0, 1.0) (i.e., does not include 1). Casting that return value to a int will always return 0.

A lot of the results I'm seeing in the search have more problems with order of operations -- so long as you multiply the random() result by a good-sized constant before you cast to int, it actually does what it's "supposed" to do.

Re: A common bug in published code

#23
post #5

Earlier quoted context omitted.

They are all in error. "(int)Math.random()" always returns 0 because Math.random() returns [0.0, 1.0) (i.e., does not include 1). Casting that return value to a int will always return 0.

A lot of the results I'm seeing in the search have more problems with order of operations -- so long as you multiply the random() result by a good-sized constant before you cast to int, it actually does what it's "supposed" to do.

This is the point of the post. There are no examples that aren't casting Math.random() to an int before doing anything with it thus using a pretty expensive way to represent 0.

Re: A common bug in published code

#24
post #13

Someone should write a tool that scrapes these results and automatically files bugs at the associated bug trackers for open-source projects.

I've been thinking of doing something and i'm looking for people to work with! Notes here: http://memeschemes.com/code_search/

Re: A common bug in published code

#28

A-mazing Also, notice the difference in number of results between C and C++ below: http://www.google.com/codesearch?hl=en&lr=&q=if%5Cs*... vs http://www.google.com/codesearch?hl=en&lr=&q=if%5Cs*...

That's why I write comparisons like

if (CONSTANT == variable)

Re: A common bug in published code

#30
post #4
post #3

The first few examples are fine. It's the unscaled ones later on, like "longarr[i] = (int) Math.random();" and especially "[Math.abs((int) Math.random()) % 3];" where the authors didn't notice that random() "returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0." Here's another extreme example: (int) Math.random() / Integer.MAX_VALUE % (maxScoreCount + 1); I eyeballed that about…

That's not true. Try running the following code: public class Test { public static void main(String[] args) { System.out.println("Test: " + (int)Math.random() * 100); System.out.println("Test: " + (int)(Math.random() * 100)); } } My results, from repeated tests: java Test Test: 0 Test: 59 java Test Test: 0 Test: 18 java Test Test: 0 Test: 72 java Test Test: 0 Test: 11

It is random! http://xkcd.com/221/
Post reply on HN