8 ms·
A common bug in published code
- nicetryguy 16y agoim floored
- modulow 16y agoi see what you did there...
- mattdeboard 16y agoYeah, I nearly hit the ceiling myself.
- richardw 16y agoRandom ints between 0 and 1 offer a pretty limited range of jokes, I guess. http://en.m.wikipedia.org/wiki/Range_(computer_science) http://en.m.wikipedia.org/wiki/Range_(computer_science) Edit: added range def because of downvotes. Sigh
- mturmon 16y agoYour pun is roundly appreciated.
- deleted 16y ago[deleted]
- dalke 16y agoThe 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 5% of the calls are in error.
- nbpoole 16y agoThat'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
- deleted 16y ago[deleted]
- electrum 16y agoIt is random! http://xkcd.com/221/ http://xkcd.com/221/
- jimm 16y agoThey 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.
- jerfelix 16y agoThey are not in error if you are trying to get a 0. This is yet another good technique for obfuscated code.
- nbpoole 16y agoIndeed. Take a look at http://www.google.com/codesearch/p?hl=en#TXS-ndgbCls/trunk/junit/org/homeunix/thecave/buddi/test/model/TransactionTest.java&q=%5C(int%5C)%5Cs*Math%5C.random%5C(%5C)&sa=N&cd=20&ct=rc&l=56 http://www.google.com/codesearch/p?hl=en#TXS-ndgbCls/trunk/j... for instance: if you weren't looking for an incorrect cast, it would be very hard to notice. Two out of three casts are correct: only one is missing the parentheses.
- 16y ago
- ilcavero 16y agothe main cause here is that randomness is hard to test so that code was just assumed to be correct. With randomness you can never be sure, http://www.random.org/analysis/ http://www.random.org/analysis/
- klodolph 16y agoYes, but... (int)Math.random() always returns zero. A little suspicious.
- polynomial 16y agoYou can never be sure the next one won't be a 1!
- ignifero 16y agoA-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*%5C%28%5Cs*%5Cw%2B%5Cs*%3D%5Cs*%5Cd%2B%5Cs*%5C%29+file%3A^.*%5C.c%24&sbtn=Search http://www.google.com/codesearch?hl=en&lr=&q=if%5Cs*... vs http://www.google.com/codesearch?hl=en&lr=&q=if%5Cs*%5C%28%5Cs*%5Cw%2B%5Cs*%3D%5Cs*%5Cd%2B%5Cs*%5C%29+file%3A^.*%5C.cpp%24&sbtn=Search http://www.google.com/codesearch?hl=en&lr=&q=if%5Cs*...
- benburleson 16y agoThat's why I write comparisons like if (CONSTANT == variable)
- klochner 16y ago"yoda conditions": http://stackoverflow.com/questions/2349378/new-programming-jargon-you-coined/2430307#2430307 http://stackoverflow.com/questions/2349378/new-programming-j...
- derleth 16y agoYoda conditions, those are called. http://united-coders.com/christian-harms/what-are-yoda-conditions http://united-coders.com/christian-harms/what-are-yoda-condi...
- Splines 16y agoI 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.
- nostrademons 16y agoI 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.
- 16y ago
- samps 16y agoSomeone should write a tool that scrapes these results and automatically files bugs at the associated bug trackers for open-source projects.
- bnewbold 16y agoI've been thinking of doing something and i'm looking for people to work with! Notes here: http://memeschemes.com/code_search/ http://memeschemes.com/code_search/
- mbenjaminsmith 16y agoA bug in that program would certainly be wonderful. Have fun when it files a few million unnecessary bug reports while you're sleeping.
- mail2345 16y agoHave it require approval for each bug report. While it isn't as fast as a fully automatic bug finding system, it is faster than a manual find and file. Plus it takes care of those situations where it was intended(poor coding, but it works).
- bobds 16y agoI've had some similar ideas myself, consider me interested. I've written some (very little) code to search projects among the big open source hosts (SF, Google, Github etc). Also have a nice domain for it.
- wladimir 16y agoThis 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)
- endian 16y agoI've been wanting to do that for: * NPM packages whose package.json file is missing a link to the package's GitHub repo * Markdown READMEs without the right file extension (for GitHub)
- deleted 16y ago[deleted]
- ghewgill 16y agoLast year I found several instances of the printf format "200%d" when dealing with years. Some still exist: http://hewgill.com/journal/entries/548-y2010-bugs-found-via-google-code-search http://hewgill.com/journal/entries/548-y2010-bugs-found-via-...
- dgallagher 16y agoI think that's called job security in some circles. ;)
- baltcode 16y agoApparently, python only has 5 instances of the corresponding error: http://www.google.com/codesearch?hl=en&lr=&q=\s%2Bint\s*\(\s*random\.random\(\)\s*\)&sbtn=Search http://www.google.com/codesearch?hl=en&lr=&q=\s%2Bin... Python-Java flame-war, anyone?
- rbonvall 16y agoThere is no need to cast to int in Python: a = random.randrange(10000)
- myelin 16y agoSlight 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 error here is assuming that Math.random returns something outside [0.0, 1.0). This is the error that all five of the Python examples are showing. (Boo to Python AND Java programmers.)
- ZoFreX 16y agoThird 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 simple to do it right; Random#nextInt(int) "Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)" (also consider using SecureRandom).
- calpaterson 16y agoI actually struggle to understand why people even use Math.random() when the Random class exists.
- Adrock 16y agoSee the previous HN discussion: http://news.ycombinator.com/item?id=960886 http://news.ycombinator.com/item?id=960886
- deleted 16y ago[deleted]
- __mark 16y agoOh, so it always rounds down to zero, that's pretty bad. Casting it to a double would help right?
- ChuckMcM 16y agoOne 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.
- derleth 16y ago> queue Speaking of common bugs... (The word you're looking for is 'cue'.)
- redthrowaway 16y agoUnless we're talking about adding the debate to the queue of debates we're having, that is. Even then, I suppose, it should be enqueue...
- nolanw 16y agoWere you actually confused, or are you just picking nits? If the latter, then it's not really a bug is it.
- kwantam 16y agoActually, nits are bugs. They just haven't hatched yet. [1] http://en.wikipedia.org/wiki/Louse http://en.wikipedia.org/wiki/Louse
- Carlfish 16y agoI believe this is the tool you are looking for. http://findbugs.sourceforge.net/bugDescriptions.html#RV_01_TO_INT http://findbugs.sourceforge.net/bugDescriptions.html#RV_01_T...
- nradov 16y agoI'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.
- rmccue 16y agoFor those who haven't picked up the error: Math.random() returns a value between 0 and 1, and the typecasting floors the value.
- ghempton 16y agoMajority of these results are examples and test cases which is probably why the bugs have not manifested themselves.
- merloen 16y agoAny other examples of bugs that you can search for?