5 ms·
Apparently, 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=Se
by baltcode 15y ago
Apparently, 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?
- myelin 15y 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 15y 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 15y agoI actually struggle to understand why people even use Math.random() when the Random class exists.
- Dilpil 15y agoAlot of people probably look for Random but find Math.random().
- nostrademons 15y agoDon't need an import for Math.random(). Random is in java.util, while Math is in java.lang.
- anonymoushn 15y agopublic class example { public static void main(String[] JAVA_LOL) throws Throwable { // prints "6" System.out.println((int)6.99999); } }
- ZoFreX 15y agoYou (and everyone else) are of course right. I remembered the wrong thing, what I said doesn't apply to that method in Java. What does apply is that there are lots of subtleties in generating random numbers, and there's rarely a reason to re-invent the wheel. There's a good example of how subtle pseudorandomness is in the Java documentation, so I will link that instead of embarrassing myself further: http://download.oracle.com/javase/1.4.2/docs/api/java/util/Random.html#nextDouble() http://download.oracle.com/javase/1.4.2/docs/api/java/util/R...
- thedufer 15y agoWe're all aware that casting to an integer does truncation, not rounding. Right?
- GVRV 15y agoAt least in Java and other languages, when casting as int truncates, and doesn't round the number, so: ((int) (0.9999999)) == 0 But then there's Math.floor and Math.ceil for what you're describing which can be used as well.
- MBlume 15y agoimport random # will print an integer between 0 and 10 inclusive print randint(0,10)
- llimllib 15y ago> I can't speak for Python, but in Java it's quite simple to do it right http://docs.python.org/library/random.html#random.randint http://docs.python.org/library/random.html#random.randint
- streety 15y agoActually the python equivalent of "Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)" would be random.randrange(0, n, 1) random.randint(a, b) returns a uniformly distributed int value between a (inclusive) and b (inclusive). </nitpick>
- dangoldin 15y agoIn that case you can always use floor or ceil.
- deleted 15y ago[deleted]
- rbonvall 15y agoThere is no need to cast to int in Python: a = random.randrange(10000)
- njharman 15y agoI can help that flame war... 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.
- mirkules 15y agoClearly, PHP is a better language than both: int rand($min, $max) 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.