Skip to content

Conversation

konard
Copy link
Member

@konard konard commented Sep 14, 2025

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 using Lazy<T>
    • ThreadLocal property: Thread-local Random instances for multi-threaded scenarios
    • Create() method: Creates new Random instances with system time seed
    • Create(int seed) method: Creates new Random instances with specific seed
    • CreateSecure() method: Creates cryptographically secure random number generators
  • Full backwards compatibility: The existing RandomHelpers.Default property continues to work exactly as before

  • Comprehensive test coverage: Added tests for all new factory methods including thread safety verification

Key Improvements

  1. Thread Safety:

    • Default property now uses Lazy<T> for thread-safe lazy initialization
    • ThreadLocal property provides thread-local instances for concurrent scenarios
  2. Flexibility: Multiple factory methods allow choosing the most appropriate Random instance for different use cases

  3. Security: CreateSecure() method provides access to cryptographically secure random generation

  4. Performance: Lazy initialization prevents unnecessary object creation until actually needed

Test Plan

  • All existing tests continue to pass (backwards compatibility verified)
  • New tests cover all factory methods
  • Thread safety tests verify ThreadLocal behavior
  • Seed-based factory method tests verify deterministic behavior
  • Secure random factory method tests verify proper instantiation

Fixes #5

🤖 Generated with Claude Code

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: #5
@konard konard self-assigned this Sep 14, 2025
- Replace static readonly field with Lazy<T> property for thread-safe lazy initialization
- Add ThreadLocal<Random> property for thread-safe multi-threaded scenarios
- Add factory methods Create() and Create(int seed) for explicit instance creation
- Add CreateSecure() method for cryptographically secure random generation
- Maintain full backwards compatibility with existing Default property access
- Update comprehensive test coverage for all new factory methods

Fixes #5

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@konard konard changed the title [WIP] Think about a better way to access factories Implement better factory access patterns for Random class Sep 14, 2025
@konard konard marked this pull request as ready for review September 14, 2025 10:36
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.

Think about a better way to access factories

1 participant