Live data from Hacker News

A common bug in published code

google.com

61–70 of 79 posts

Re: A common bug in published code

#61
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.

> 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.

Yeah but since cast binds tighter than multiplication, any code following the pattern `(int) Math.random() * a` is broken and generates 0 every single time.

For the code to work, you need `(int) (Math.random() * a)`: http://www.google.com/codesearch?q=\(int\)\s*\(\s*Math\.rand...

Re: A common bug in published code

#62
post #34

Earlier quoted context omitted.

Third error: This is not the correct way to randomly pick a number in a set range. The proper way is actually quite complicated. Imagine you do (int) (Math.random() * 10), this could give you numbers from 0 to 10. However, you only get 0 if Math.random() * 10 is less than 0.5, but you get 1 if the value is between 0.5 and 1.5. You are half as likely to see a zero! I can't speak for Python, but in Java it's quite simp…

I actually struggle to understand why people even use Math.random() when the Random class exists.

Alot of people probably look for Random but find Math.random().

Re: A common bug in published code

#63

One would hope at some point a tool would warn you about this since clearly it could optimize (int) Math.random() to just 0. [queue debate about tools that hold your hands vs understanding what you are actually writing] I wonder if they did the search for if (x = y) bug pre-gcc-4.x-warn what sort of numbers they would get.

"queue debate about tools that hold your hands vs understanding what you are actually writing"

For a start, explain why, in Java, (int) Math.random() is a bug. A programmer that happens upon this thread, now knows "this is bad", but has no clue why. A tool will only make the programmer even more reliant on tools and IDEs, so this kind of bug will be eliminated, but the frame of mind that spawned it will live on.

Re: A common bug in published code

#65

One would hope at some point a tool would warn you about this since clearly it could optimize (int) Math.random() to just 0. [queue debate about tools that hold your hands vs understanding what you are actually writing] I wonder if they did the search for if (x = y) bug pre-gcc-4.x-warn what sort of numbers they would get.

I believe this is the tool you are looking for. http://findbugs.sourceforge.net/bugDescriptions.html#RV_01_T...

I'm amazed to still find Java programmers who don't run Findbugs on all their code. It's free, fast, and has a low rate of false positives.

Re: A common bug in published code

#66
post #56

Earlier quoted context omitted.

That's why I write comparisons like if (CONSTANT == variable)

I know it's safer, but personally I find it harder to read. Especially if the constant is a #define or enum and is similar in format to the variable in question.

I personally like Yoda Conditions, but a few coworkers have pointed out that any linter worth its salt will catch an = inside a conditional, so this doesn't actually gain much in safety. I've stopped using them because there a large number of coworkers find them confusing.

Re: A common bug in published code

#67
post #34

Earlier quoted context omitted.

Third error: This is not the correct way to randomly pick a number in a set range. The proper way is actually quite complicated. Imagine you do (int) (Math.random() * 10), this could give you numbers from 0 to 10. However, you only get 0 if Math.random() * 10 is less than 0.5, but you get 1 if the value is between 0.5 and 1.5. You are half as likely to see a zero! I can't speak for Python, but in Java it's quite simp…

I actually struggle to understand why people even use Math.random() when the Random class exists.

Don't need an import for Math.random(). Random is in java.util, while Math is in java.lang.

Re: A common bug in published code

#68

im floored

Yeah, I nearly hit the ceiling myself.

Random ints between 0 and 1 offer a pretty limited range of jokes, I guess.

http://en.m.wikipedia.org/wiki/Range_(computer_science) Edit: added range def because of downvotes. Sigh

Re: A common bug in published code

#69
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/

This will be very useful as long as it finds confirmed bugs. Otherwise it will be more like an unasked-for code style check (For example, one can argue using functions like strcpy are unsafe, but unless it's really possible to get too many characters in the buffer, it's not a bug)

Re: A common bug in published code

#70
post #34
post #20

Earlier quoted context omitted.

Slight difference here. There are actually two types of errors in the Java code: 1. int foo = (int) Math.random() * some_max_value; The error here is assuming that the multiplication takes place before the truncation. This isn't happening in the Python code because int(expression to truncate) is unambiguous. (+1 to Python here for making it hard to shoot yourself in the foot). 2. int foo = (int) Math.random(); The er…

Third error: This is not the correct way to randomly pick a number in a set range. The proper way is actually quite complicated. Imagine you do (int) (Math.random() * 10), this could give you numbers from 0 to 10. However, you only get 0 if Math.random() * 10 is less than 0.5, but you get 1 if the value is between 0.5 and 1.5. You are half as likely to see a zero! I can't speak for Python, but in Java it's quite simp…

[deleted]
Post reply on HN