Skip to content

Conversation

@210-reverof
Copy link
Contributor

Summary

Implement DeterministicRandom class with seed-based Random instance caching for deterministic test reproducibility.

Description

  • Created DeterministicRandom class that extends Random
    • Automatically caches Random instances using nextLong() generated values as keys
    • Provides getRandomInstance(long seed) to retrieve cached Random instances by seed
    • Provides getCurrentSeedRandom() to get the Random instance for the current seed
    • Implements cache size management with a limit of 32 entries to prevent memory leaks
  • Added 7 comprehensive property-based tests using JQwik to verify caching behavior and requirements

How Has This Been Tested?

  • Each test runs 1000 iterations with different seed values (7000 total test cases)
  • Build verification: ./gradlew :fixture-monkey-api:test passes successfully
  • Verified: cache creation on nextLong(), instance retrieval, cache size limit (≤32), deterministic behavior, and seed independence

Is the Document updated?

Not yet.

import org.apiguardian.api.API.Status;

@API(since = "1.1.16", status = Status.MAINTAINED)
public class DeterministicRandom extends Random {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to create a new interface for obtaining the Random instance and the seed value.

It is to avoid using the DeterministicRandom directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your feedback! 🙂

I created a new interface named RandomSource to obtain Random instances and seed values.
I also implemented DeterministicRandomSource that encapsulates the caching logic,
so the internal implementation is no longer exposed.

84e0308

@210-reverof 210-reverof force-pushed the add-deterministic-random-class branch from 7077b4a to 237fa16 Compare December 14, 2025 08:28
@210-reverof 210-reverof force-pushed the add-deterministic-random-class branch from 38a7176 to 84e0308 Compare December 14, 2025 08:33
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.

2 participants