A Random is created to draw a single number
What it finds
Random().nextInt(10) creates and seeds a new generator for each number. A generator kept in a property, or kotlin.random.Random, draws numbers without that cost, and with a better spread than generators seeded close together in time.
From the rule's source, random_per_call.rs.
import java.util.Random val shared = Random() fun roll(): Int { val a = Random().nextInt(6)tx0 reports: a new Random is created for one nextInt() val b = Random(42).nextInt(6) return a + b + shared.nextInt(6)} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/kotlin:random-created-per-use" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/kotlin:random-created-per-use] reason="..."tx0 explain tx0/kotlin:random-created-per-use shows the same in the terminal. Found a false positive? Report it.