2021-04-28 16:49:39 +02:00
|
|
|
There is no need to multiply the output of ``++Random++``'s ``++nextDouble++`` method to get a random integer. Use the ``++nextInt++`` method instead.
|
|
|
|
|
|
|
|
|
|
|
|
This rule raises an issue when the return value of any of ``++Random++``'s methods that return a floating point value is converted to an integer.
|
|
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
|
|
|
|
----
|
|
|
|
Random r = new Random();
|
|
|
|
int rand = (int)r.nextDouble() * 50; // Noncompliant way to get a pseudo-random value between 0 and 50
|
|
|
|
int rand2 = (int)r.nextFloat(); // Noncompliant; will always be 0;
|
|
|
|
----
|
|
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
|
|
|
|
----
|
|
|
|
Random r = new Random();
|
|
|
|
int rand = r.nextInt(50); // returns pseudo-random value between 0 and 50
|
|
|
|
----
|