Standard Analyses

SemmleCode Professional comes with a wide range of pre-packaged analyses, from architectural properties and metrics, to statement-level checks for likely bugs and violations of best practice.


Random used only once

This query finds code that creates a new instance of class java.util.Random, but uses it only once. According to the API specification

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers.
Thus every such use will return the same "random" number.

How to Interpret the Query Results

The query flags any method invocation whose receiver is a class instance expression creating an object of class java.util.Random, i.e. code like

    new Random().m()
It also display the list of generated warnings in the result view.

How to Address the Query Results
Source Code
import default

from MethodAccess ma, Method random
where random.getDeclaringType().hasQualifiedName("java.util","Random") and
      ma.getMethod() = random and
      ma.getQualifier() instanceof ClassInstanceExpr
select ma, "Random object created and used only once"
References