8 ms·
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];"
by dalke 16y ago
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 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.
- ubernostrum 16y agoA 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.
- spullara 16y agoThis 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.
- ubernostrum 16y agoThe comment I was replying to was simply saying that casting the return value of Math.random() will be 0. Which is true, but is not the issue -- order of operations (for people who think the multiplication happens before the cast) is the issue.
- masklinn 16y ago> 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\.random\(\) http://www.google.com/codesearch?q=\(int\)\s*\(\s*Math\.rand...
- deleted 16y ago[deleted]
- deleted 16y ago[deleted]
- fjh 16y agoHave a look at the precedence table for Java: http://introcs.cs.princeton.edu/11precedence/ http://introcs.cs.princeton.edu/11precedence/ 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.
- dalke 16y agoOh, wow! I didn't know that about Java. I see then why I got all the downvotes.