Implement better factory access patterns for Random class #61
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR implements better factory access patterns for the
Random
class as requested in #5, providing multiple ways to access Random instances with improved thread safety and flexibility.Changes Made
Enhanced RandomHelpers class with multiple factory patterns:
Default
property: Thread-safe lazy initialization usingLazy<T>
ThreadLocal
property: Thread-local Random instances for multi-threaded scenariosCreate()
method: Creates new Random instances with system time seedCreate(int seed)
method: Creates new Random instances with specific seedCreateSecure()
method: Creates cryptographically secure random number generatorsFull backwards compatibility: The existing
RandomHelpers.Default
property continues to work exactly as beforeComprehensive test coverage: Added tests for all new factory methods including thread safety verification
Key Improvements
Thread Safety:
Default
property now usesLazy<T>
for thread-safe lazy initializationThreadLocal
property provides thread-local instances for concurrent scenariosFlexibility: Multiple factory methods allow choosing the most appropriate Random instance for different use cases
Security:
CreateSecure()
method provides access to cryptographically secure random generationPerformance: Lazy initialization prevents unnecessary object creation until actually needed
Test Plan
Fixes #5
🤖 Generated with Claude Code