A Random is created inside a loop, so instances made in quick succession may give the same numbers
What it finds
On .NET Framework, new Random() seeds from the clock, so instances created within the same few milliseconds produce identical sequences; on every runtime, creating one per round wastes work. One instance created before the loop, or Random.Shared, gives independent values.
From the rule's source, random_created_in_loop.rs.
using System; class Dice{ int[] Roll(int n) { var results = new int[n]; for (int i = 0; i < n; i++) { results[i] = new Random().Next(6);tx0 reports: a new Random is created on every round of the loop } return results; }} 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/csharp:random-created-in-loop" = "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/csharp:random-created-in-loop] reason="..."tx0 explain tx0/csharp:random-created-in-loop shows the same in the terminal. Found a false positive? Report it.