

A common bug in published code - RiderOfGiraffes
http://www.google.com/codesearch?hl=en&lr=&q=\(int\)\s*Math\.random\(\)

======
smanek
For non-Java heads, (int) Math.random() generates a random double from [0,1)
which is then cast as an integer (which means that it is always truncated to
0).

They probably wanted something like (int) (Math.random()*2) which will give
you 0 half the time or 1 half the time.

Although it is still a really inefficient way to generate 1-bit of entropy
(but sometimes it just isn't worth the trouble to instantiate a Random object,
so I guess I could look past that ...).

~~~
lucifer

        (new Random()).nextBoolean();  // too much trouble?
        (new Random()).nextInt(2);     // too much trouble?

~~~
gjm11
Aargh, no! That will seed the RNG on the basis of the current time. According
to the docs, "Two Random objects created within the same millisecond will have
the same sequence of random numbers" which really really isn't likely to be
what you want.

To get sensible results, you need instead to keep a single Random object
around and use it every time you need another bit. And yes, that's more
trouble than just calling Math.random and tweaking the result.

~~~
gjm11
Er, actually it looks as if exactly what this does depends on what version
you're using. The Java Platform SE 6 docs say "This constructor sets the seed
of the random number generator to a value very likely to be distinct from any
other invocation of this constructor." Same for 5. But 1.4.2 has the text I
quoted above.

In any case, creating a new Random object every time you want a random number
is unlikely to give satisfactory results. There is certainly no guarantee that
the implementation will try to make it do so.

~~~
chubbard
Reread the docs! The API hasn't changed from 1.4 to 1.6. nextInt(x)'s is used
to generate numbers from [0,x) Maybe in 1.6 they started using a better
seeding source for randomness since they added System.nanoTime(). But, your
original objection is a wrong interpretation of the API docs.

~~~
gjm11
Huh? I didn't say anything about nextInt.

The point is that if every time you want a random number you construct a new
Random object using the default constructor, then (1) if you're using version
1.4 or earlier then rapidly repeated "random" number generation will give you
the same result over and over, and (2) if you're using version 1.5 or later
then it's (quite rightly) almost entirely unspecified what you'll get and
whether it will be "random enough" for any particular purpose and whether it
will be terribly inefficient.

(I'm wondering whether perhaps you missed the fact that lucifer's proposed
constructions involve making a new Random object each time you want some
randomness.)

------
chancho
Google code search needs a "submit bug report to all" function.

------
ZitchDog
Here's a good one - assignment inside an if:
[http://www.google.com/codesearch?hl=en&lr=&q=if\\(\w%...](http://www.google.com/codesearch?hl=en&lr=&q=if\\\(\\w%2B\\s%2B%3D\\s*\\w%2B\\\)&sbtn=Search)

Not always a bug, but quite a few of these look like bugs - the first result
is especially suspect!

~~~
DrJokepu
I don't think that the first one is a bug. First, no programmer with any
experience would write (variable == true) instead of (variable). Second, there
are a number of lines commented out in that area so it's likely that that
particular code was intended as a "temporary hack". I think the intention of
the author was definitely assignment in that case, although it is expressed in
a hackish, unreadable way. Seeing the previous revisions of the same file
would be helpful.

EDIT/TLDR: It looks like it either is intentional, or written by very
unexperienced developer.

~~~
ZitchDog
It is almost certainly a bug.

Fact: mAutoCommit has a setter bean property.

mAutoCommit looks like it can be toggled to true/false to enable automatic
updating of some configuration inside the setValue(Object) method. I'm 99.9999
percent sure that the author of the code did not intend to toggle the
mAutoCommit option to 'true' each time a configuration value is set.

I will agree, however, that this is almost certainly written by an
inexperienced developer. It's not a stretch to think that the inexperienced
developer who might test a boolean with x == true might also forget an equals
sign.

------
lyso
Hmph - second hit is in the OpenJDK code. I guess the author forgot to include
braces and so randomDelay() always has a delay of 0.

~~~
vlisivka
int runs = (int) Math.random() * _1000000_ ;

is equivalent to int runs = (int) (Math.random() * 1000000);

This search is better ( "\s?[;)]" is added at the end):
[http://www.google.com/codesearch?hl=en&lr=&q=\%28int...](http://www.google.com/codesearch?hl=en&lr=&q=\\%28int\\%29\\s*Math\\.random\\%28\\%29\\s*\[%3B%29\]&sbtn=Search)

~~~
danparsonson
Actually it looks like casting (being a unary operator) takes precedence over
multiplication:

[http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op...](http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html)

[http://www.cs.uwf.edu/~eelsheik/cop2253/resources/op_precede...](http://www.cs.uwf.edu/~eelsheik/cop2253/resources/op_precedence.html)

~~~
jonsen
Right:

[http://java.sun.com/docs/books/jls/second_edition/html/expre...](http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#238146)

------
microtherion
Why is that a bug? 0 is as good a random number as any!

------
Adrock
Don't forget (long)Math.random():
[http://www.google.com/codesearch?hl=en&lr=&q=%5C%28l...](http://www.google.com/codesearch?hl=en&lr=&q=%5C%28long%5C%29%5Cs*Math%5C.random%5C%28%5C%29&sbtn=Search)

------
Kliment
This is terrible. Also, a great way to get high scores in games that use it.

~~~
zackattack
? how?

------
wisty
Also available in python:
[http://www.google.com/codesearch?hl=en&lr=&q=int\%28...](http://www.google.com/codesearch?hl=en&lr=&q=int\\%28random\\.random\\%28\\%29\\%29&sbtn=Search)

Only 7 matches though. 1 false positive (print(random.random()) in python
itself, and no real bugs in projects that I recognize.

~~~
paulbaumgart
A minor tweak to your search to fix the false positives:
[http://www.google.com/codesearch?q=[^\w]int\%28random\\.rando...](http://www.google.com/codesearch?q=\[^\\w\]int\\%28random\\.random\\%28\\%29\\%29)

------
omouse
So what's the bug? Casting?

