diff --git a/docs/docs/00200-core-concepts/00200-functions/00300-reducers/00400-reducer-context.md b/docs/docs/00200-core-concepts/00200-functions/00300-reducers/00400-reducer-context.md index ac5374516e5..a6fb67f68ba 100644 --- a/docs/docs/00200-core-concepts/00200-functions/00300-reducers/00400-reducer-context.md +++ b/docs/docs/00200-core-concepts/00200-functions/00300-reducers/00400-reducer-context.md @@ -272,6 +272,46 @@ The context provides access to a random number generator that is deterministic a Never use external random number generators (like `Random` in C# without using the context). These are non-deterministic and will cause different nodes to produce different results, breaking consensus. ::: +Use the context-provided random API for any reducer logic that needs random values: + + + + +```typescript +const fraction = ctx.random(); // [0.0, 1.0) +const roll = ctx.random.integerInRange(1, 6); // inclusive +const bytes = ctx.random.fill(new Uint8Array(16)); +``` + + + + +```csharp +double fraction = ctx.Rng.NextDouble(); // [0.0, 1.0) +int roll = ctx.Rng.Next(1, 7); // [1, 7) +``` + + + + +```rust +use spacetimedb::rand::Rng; + +let value: u32 = ctx.random(); +let roll: u32 = ctx.rng().gen_range(1..=6); +``` + + + + +```cpp +auto& rng = ctx.rng(); +int32_t roll = rng.gen_range(1, 6); // inclusive +``` + + + + ## Module Identity The context provides access to the module's own identity, which is useful when a reducer needs to refer to the database itself.