A common bug in published code
google.com
google.com
* 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)
int runs = (int) Math.random() * 1000000;
You'd do
int runs = (int) (Math.random() * 1000000);
[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.
Speaking of common bugs...
(The word you're looking for is 'cue'.)
http://findbugs.sourceforge.net/bugDescriptions.html#RV_01_T...
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.
"A random value from 0 to 1 is being coerced to the integer value 0. You probably want to multiple the random value by something else before coercing it to an integer, or use the Random.nextInt(n) method."
That's about as good an explanation as you'd get from anyone.
The way I see it is this. Simple bugs like this happen because _human beings are flawed._ All it takes is a momentary lapse of concentration and you've put the cast in the wrong place in a method you don't write a test for because you're in a hurry, and all it's doing is generating a random number with a standard API so why bother? (Or it's something you wouldn't normally even test for, like assuming something is re-entrant when it isn't)
We're inevitably going to make a certain number of mistakes a day, and it's our duty to put systems into place that catch those mistakes before they cause any more damage than they should.
Python-Java flame-war, anyone?
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.)
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).
public class example
{
public static void main(String[] JAVA_LOL) throws Throwable
{
// prints "6"
System.out.println((int)6.99999);
}
}random.randint(a, b) returns a uniformly distributed int value between a (inclusive) and b (inclusive).
</nitpick>
a = random.randrange(10000)I bet those 5 were written by folks with more (or stronger) skill with C/C++/Java other strongly typed language where casting is required/common.
Casting is uncommon and "weird" in Python. Usually means you're being unpythonic. As in this case you should be using randint or randrange rather than cast to int.
ducks
All joking aside, I'm just pointing out the absurdity of comparing one language to another based on how easy it is to shoot oneself in the foot.
http://en.m.wikipedia.org/wiki/Range_(computer_science) Edit: added range def because of downvotes. Sigh
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
if (CONSTANT == variable)
http://stackoverflow.com/questions/2349378/new-programming-j...
http://united-coders.com/christian-harms/what-are-yoda-condi...
Here's another extreme example: (int) Math.random() / Integer.MAX_VALUE % (maxScoreCount + 1);
I eyeballed that about 5% of the calls are in error.
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: 11Yeah 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...
The cast has higher precedence than multiplication, so the scaling doesn't help at all, unless you use parenthesis to force the right order of evaluation.