{"record":{"id":"e7c15a1a2d45cacc","repo":"clockworklabs/SpacetimeDB","slug":"math-random-is-not-available-in-spacetimedb-module","errorCode":null,"errorMessage":"Math.random is not available in SpacetimeDB modules. Use ctx.random instead.","messagePattern":"Math\\.random is not available in SpacetimeDB modules\\. Use ctx\\.random instead\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"crates/core/src/host/v8/builtins/delete_math_random.js","lineNumber":6,"sourceCode":"delete Math.random;\nObject.defineProperty(Math, 'random', {\n  enumerable: false,\n  configurable: true,\n  get() {\n    throw new TypeError(\n      'Math.random is not available in SpacetimeDB modules. Use ctx.random instead.'\n    );\n  },\n});\n","sourceCodeStart":1,"sourceCodeEnd":11,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/6dee26c6efc2856793e12b148a59742964f5d783/crates/core/src/host/v8/builtins/delete_math_random.js#L1-L11","documentation":"The V8 module host installs a builtin that deletes Math.random and reinstalls it as a throwing getter. SpacetimeDB reducers must be deterministic — database state is derived from ordered reducer execution — so unseeded randomness is banned. Modules get a deterministic, seed-based generator on ctx.random instead (seeded per call from the module context).","triggerScenarios":"Evaluating or calling Math.random anywhere in a TypeScript module (reducer, scheduled function, client_authority/init) running under the SpacetimeDB V8 host; accessing Math.random indirectly through third-party npm code — uuid-style generators, lodash.sample, chance.js, game RNG helpers — imported into the module.","commonSituations":"Porting game or server logic that used Math.random; libraries that mint random ids; code that passes under plain Node during development but throws once published and invoked in spacetimedb.","solutions":["Replace Math.random() with the context API: ctx.random() (float in [0,1)), ctx.random.integerInRange(min, max), ctx.random.uint32(), ctx.random.fill(Uint8Array), ctx.random.bigintInRange(min, max)","Derive ids from ReducerContext data or ctx.random.fill(new Uint8Array(16)) instead of random-string helpers","Audit module dependencies for Math.random usage and swap in deterministic equivalents","Test modules under the SpacetimeDB host during development, not only under Node, so violations surface early"],"exampleFix":"// before\nexport const rollDice = (ctx: ReducerContext) => {\n  const roll = Math.floor(Math.random() * 6) + 1; // TypeError in module\n};\n\n// after\nexport const rollDice = (ctx: ReducerContext) => {\n  const roll = ctx.random.integerInRange(1, 6); // deterministic, seeded\n};","handlingStrategy":"fallback","validationCode":"# CI guard: reject module sources that reference the deleted builtin\n# (the getter throws at access time, so no runtime probe is safe)\ngrep -rn \"Math\\\\.random\" module-src/ && { echo 'Math.random is forbidden in SpacetimeDB modules'; exit 1; } || exit 0","typeGuard":null,"tryCatchPattern":"// acceptable only while porting; production code must be deterministic:\nlet r: number;\ntry {\n  r = Math.random();\n} catch {\n  r = ctx.random();\n}","preventionTips":["Always read randomness through ctx.random (callable plus integerInRange/uint32/fill/bigintInRange)","Audit bundled npm dependencies for Math.random before publishing the module","Run module tests inside the SpacetimeDB host, not plain Node"],"tags":["module","v8","determinism","random","typescript"],"backgroundTag":"deterministic-sandbox-restriction","analyzedSha":"6dee26c6efc2856793e12b148a59742964f5d783","analyzedAt":"2026-08-20T06:08:37.179Z","contentChangedAt":"2026-08-20T06:08:37.179Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}