-2

Suppose I write

System.out.println (Math.random()*5);

then one would obtain and output of x in [0, 4.9...]. But upon casting it as an integer, the result I continuously see (in my course) is [0, 4]. My question is how we are defining the rounding function; I am familiar with the floor function, and the floor of 4.9... is precisely 5 due to there existing no epsilon larger than zero satisfying the output x existing in some epsilon neighborhood; i.e., the equality 4.9... = 5 suffices and because the floor of an integer is that integer, the result would be 5.

Where am I going wrong here?

2 Answers2

4

Writing a new answer to address questions raised in comments. The output of Math.random() is in the range [0,1). The result will be a number strictly less than 1, so Math.random()*5 will give a result in the range [0,5), that is a number strictly less than 5. Since casting truncates, that means that your results will be in the integer set {0, 1, 2, 3, 4} with (approximately) equal probabilities for each of the five values.

Java: Math.random() Max Value (double just less than 1) has some more details on the math of the exact values that are possible with Math.random().

Don Hosek
  • 981
  • 6
  • 23
2

When you cast to int like this

int i = (int) 4.9;

JVM will simply drop decimal places and you will get value 4 assigned to the variable. It is not rounding it is truncating,

user207421
  • 305,947
  • 44
  • 307
  • 483
Ivan
  • 8,508
  • 2
  • 19
  • 30
  • 4.99... repeating is what I am asking, not 4.9. –  Oct 28 '20 at 03:05
  • The floor of 4.99 repeating is 5. You can verify with Wolfram alpha –  Oct 28 '20 at 03:06
  • @user143 In all the computer libraries I know of, the floor of 4.99 repeating is 4. Again, computer math is not the same as theoretical math, expect in such systems as Wolfram. – NomadMaker Oct 28 '20 at 03:10
  • @NomadMaker I do not know how one differentiates between the two; the floor function is not theoretical, nor is the equality of 4.9... = 5. How does one define the floor function in computer math? –  Oct 28 '20 at 03:12
  • As other people have said, in computer libraries, floor just truncates. This happens because there is no way to write 4.99 infinitely repeating. – NomadMaker Oct 28 '20 at 03:13
  • No, it happens because `floor()` is *supposed* to truncate. Nothing to do with recurrng decimal places. – user207421 Oct 28 '20 at 03:15
  • 1
    @NomadMaker Well, one could devise a representation of repeating decimals using some other number representation, e.g. geometric series. A computer algebra system could do it. But specifically to answer OP's question, a `double` in Java is actually a rational number, and whether it's less, equal, or greater than 4 is unambiguous. – Robert Dodier Oct 28 '20 at 03:51
  • 1
    @user143 A web search for IEEE 754 will turn up the resources you need. Java and other languages represent decimal numbers as rational approximations of the form (2^exponent) times (nnnnn) where nnnnn are the significant bits and 2^exponent is a scaling factor. There are many other representations, but IEEE 754 is built into hardware, so it is the default. Java `float` and `double` are 32 and 64 bit versions of that. There are other, software-only number representations for Java, such as BigDecimal and BigInteger, and many other representations for other languages. – Robert Dodier Oct 28 '20 at 03:58