Skip to content

Issue #1487: reuse Random instance#1489

Merged
thboileau merged 1 commit into2.7from
tboileau/2.7_Issue-1487_reuse_Random_instance
Mar 8, 2026
Merged

Issue #1487: reuse Random instance#1489
thboileau merged 1 commit into2.7from
tboileau/2.7_Issue-1487_reuse_Random_instance

Conversation

@thboileau
Copy link
Copy Markdown
Contributor

The aim

Creating a new Random object each time a random value is needed is inefficient and may produce numbers that are not random, depending on the JDK. For better efficiency and randomness, create a single Random, store it, and reuse it.

The Random() constructor tries to set the seed with a distinct value every time. However, there is no guarantee that the seed will be randomly or uniformly distributed. Some JDK will use the current time as seed, making the generated numbers not random.

The solution

Use an instance member.

Check-list

  • PR size
    • Under 300 lines ✅
    • Can't be split without complicating the process even more
  • Tests
    • Added
    • Not applicable (why?)
  • Doc
    • Added
    • Not applicable
  • Reviewer
    • Asked for a review
    • Added label DO NOT REVIEW

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Mar 8, 2026

@thboileau thboileau merged commit 21d990e into 2.7 Mar 8, 2026
3 checks passed
@thboileau thboileau deleted the tboileau/2.7_Issue-1487_reuse_Random_instance branch March 8, 2026 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant