--- action: context tool: (edit|write) event: after name: use-random-service description: Use Random service instead of Math.random() glob: '**/*.{ts,tsx}' detector: ast pattern: Math.random() level: warning suggestSkills: - effect-testing --- # Use Random Service Instead of `Math.random()` ```haskell -- Transformation mathRandom :: IO Float -- impure, non-deterministic random :: Effect Float Random -- explicit dependency, testable -- Random operations next :: Effect Float Random nextInt :: Effect Int Random nextRange :: (Int, Int) → Effect Int Random shuffle :: [a] → Effect [a] Random ``` ```haskell -- Pattern bad :: IO Int bad = floor (Math.random * 100) -- R = ∅, untestable good :: Effect Int Random good = Random.nextIntBetween 0 100 -- R ⊃ Random, deterministic in tests -- Repeatable sequence in tests seeded = good & Random.withSeed "example-seed" ``` `Math.random()` bypasses Effect's random service. Use `Random.withSeed` for repeatable sequences in tests, or provide a controlled `Random.Random` service when the exact generated values matter. `Random.nextIntBetween(min, max)` includes both endpoints by default; pass `{ halfOpen: true }` to exclude the upper bound.