== Why is this an issue?

Generating random floating point values to cast them into integers is inefficient.
A random bounded integer value can be generated with a single proper method call.
Use `nextInt` to make the code more efficient and the intent clearer.


=== Noncompliant code example

[source,java,diff-id=1,diff-type=noncompliant]
----
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

[source,java,diff-id=1,diff-type=compliant]
----
Random r = new Random();
int rand = r.nextInt(50);  // returns pseudo-random value between 0 and 50
int rand2 = 0;
----


ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

Use "java.util.Random.nextInt()" instead.


'''
== Comments And Links
(visible only on this page)

=== on 21 Nov 2024, 16:48:00 Alban Auzeill wrote:
[test-code-support-investigation-for-java] Decision for scope: Main -> All.

=== on 11 Oct 2014, 11:58:06 Freddy Mallet wrote:
Sounds good for me !

endif::env-github,rspecator-view[]
