Files
openide/java/java-impl/resources/inspectionDescriptions/MathRandomCastToInt.html
Leonid Shalupov 40795fe787 IJI-2422: community/java: move resources under resources root
GitOrigin-RevId: 8b2b63fc6db476ca0c2cfe5cadd84db6c4236d0f
2025-02-05 04:43:28 +00:00

20 lines
807 B
HTML

<html>
<body>
Reports calls to <code>Math.random()</code> which are immediately
cast to <code>int</code>.
<p>Casting a <code>double</code> between <code>0.0</code> (inclusive) and
<code>1.0</code> (exclusive) to <code>int</code> will always round down to zero. The value
should first be multiplied by some factor before casting it to an <code>int</code> to
get a value between zero (inclusive) and the multiplication factor (exclusive).
Another possible solution is to use the <code>nextInt()</code> method of
<code>java.util.Random</code>.</p>
<p><b>Example:</b></p>
<pre><code> <b>int</b> r = (<b>int</b>)Math.random() * 10;
</code></pre>
<p>After the quick fix is applied:</p>
<pre><code> <b>int</b> r = (<b>int</b>)(Math.random() * 10);
</code></pre>
<!-- tooltip end -->
</body>
</html>