I'm new to java and I stumbled on the Math class. I was wondering if there was a difference between Math.random() *100 verses Math.random(100)? Would both outputs be a number between 0-99 or would Math.random(100) output a number between 0-100? Thank you!
Asked
Active
Viewed 816 times
-5
-
5There is no method in `Math` called `random` that takes any parameters; there is only the one that takes no parameters. – rgettman Nov 12 '18 at 21:10
-
you should carefully take a look to the doc.... – ΦXocę 웃 Пepeúpa ツ Nov 12 '18 at 21:11
-
1Definitely recommend trying to run code as a first step – Ben Jones Nov 12 '18 at 21:11
-
What do the docs for `Math.random()` vs `Math.random(100)` say (hint one doesn't exist)? Do the docs say the random number max is inclusive (0-99) or exclusive (0-100)? If I remember correctly `Math.random()` returns a random float from 0 to 1. Does multiplying every possible float from 0 to 1 result in a number between 0 and 100? – MeetTitan Nov 12 '18 at 21:13
1 Answers
1
Math.random()
exists. Math.random(int)
does not exist.
You may be getting that mixed up with the Random
class constructor, which takes a long
as a seed value, meaning your results will be pseudorandom and by consequence, repeatable.
If you wanted a number between 0 and 99 I actually would recommend that you use Random
. You can leverage random.nextInt(100)
to get a value between 0 and 99. Multiplying floats gets dicey very quickly, since Math.random()
only produces a floating-point number.

Makoto
- 104,088
- 27
- 192
- 230