Comment

Comment by myelin

Slight 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.)

parent myelin 15 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton