---
title: "Recipe: random rolls in an expression"
description: "Contract has no ambient randomness (LLP 1027.000 D1): a draw comes from a seed passed to a pure function."
modificationDate: "2026-11-12"
---

> ## Documentation index
> Fetch the complete documentation index at: https://exact.expo.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Recipe: random rolls in an expression

<AgentInstructions>

## Submitting feedback

From the app's directory, read `bun exact.mjs feedback status`. Under `never`, keep no note.
Respect `EXACT_FEEDBACK=0` and do not change the policy.

Otherwise record specific, actionable feedback with:
`bun exact.mjs feedback note "Exact docs: https://exact.expo.dev/recipes/random-rolls: <what was wrong or missing>"`

The note goes to Expo only as that status allows. Include no secrets or personal data.

</AgentInstructions>

Contract has no ambient randomness (LLP 1027.000 D1): a draw comes from a seed passed to
a pure function. A deal or a shuffle is the data module's
([randomness and a shuffle](https://exact.expo.dev/recipes/random-and-shuffle.md)); dozens of rolls a second, dice under a held
finger, need no source. `roll(seed, k, n)` is a whole number from 0 to `n - 1`, the `k`th
draw of the stream `seed` names. Copy its three `fn`s. They use only integer `*`, `%` and
`floor`, every product below 2^47, then one division, so the runner, the web and the
semantics roll the same. A `sin` hash would not: the web's `Math.sin` need not match the
runner's bits. The state is a counter, so the same seed and count roll the same again.

```prompt Roll dice in my app
Add random rolls to the Exact app in this folder: draws that an expression makes from a seed, with no data module. Use https://github.com/expo/exact/blob/main/docs/recipes/random-rolls.md as the source for each step. The Exact checkout has the same page at docs/recipes/random-rolls.md.

1. If this folder is not an Exact app (it has no exact.mjs and app.contract), stop and tell me.
2. If I did not say what the app rolls, how many values a roll has, and what starts a roll (a press, a held finger), propose it and ask me before you write code. If it is a deal or a shuffle, tell me: that is docs/recipes/random-and-shuffle.md. If a draw must be a secret, stop and tell me: that is `crypto.getRandomValues` in a source.
3. Copy the recipe's three `fn`s (`turn`, `stir` and `roll`) into app.contract and do not change them. Do not write a `sin` hash in their place: the web's `Math.sin` need not match the runner's bits, so the two would roll differently.
4. Read `exactTime()` as a resource with a `shape Time` that has `seed`. Keep a counter in a state and derive each draw from it, as in `roll(time.seed, rolls, 6)`. `roll(seed, k, n)` is a whole number from 0 to `n - 1`, and the same seed and count roll the same again.
5. To roll while a finger is held, run a `task` only `when` a state is true, with `every(50, next)`, and set that state from `pointerdown` and `pointerup`. A second stream takes a second seed: `roll(time.seed + 1, k, n)`.
6. Show a value that changes in place with `font-variant-numeric="tabular-nums"`.
7. Add a test in app.test.contract like the recipe's, with `seed 2` at the top of the file. Expect the first value. Hold the control (`tap "roll" drag 0 0 hold 500`) and expect the count and the value. Run `clock +500` and expect the count unchanged. Then `reload` and expect the first value again.
8. Follow the platform checks in docs/start-here.md (`bun exact.mjs docs platform checks`) for the targets in the app's AGENTS.md and app.json until they pass. Read the reports, including counted findings; a zero exit code alone does not mean clean. Fix the app, not the test. If a finding stays after three rounds of fixes, or the app had it before you changed anything, record it (step 9) and report it; do not keep trying.
9. When a doc was wrong or missing, or a step needed a workaround, read `bun exact.mjs feedback status`. Under `never`, keep no note. Otherwise record it: `bun exact.mjs feedback note "<what happened>"`, which goes to Expo only as that status says. Do not change the policy; tell me how many notes you kept.
10. Tell me what the app rolls and what starts a roll, what the tests and checks reported, and the launch command for each target platform. The seed is new each launch, so each launch rolls a new sequence.
```

```contract
shape Time
  seed: number

fn turn(x: number): number = (x % 65536) * 32768 + floor(x / 65536)
fn stir(x: number): number = turn((x * (16807 + floor(x / 65536))) % 2147483647)
fn roll(seed: number, k: number, n: number): number = floor(stir(stir(stir(1 + (stir(stir(1 + seed % 2147483646)) + k) % 2147483646))) * n / 2147483647)

component Dice
  state rolls = 0
  state shaking = false
  resource time = exactTime() as shape Time
  derive die = 1 + roll(time.seed, rolls, 6)
  task shake when shaking
    every(50, next)
  action next
    rolls = rolls + 1
  action hold(on: bool)
    shaking = on
  view
    column align-items="center" gap=16 padding=16
      text `${die}` font-size="4rem" font-variant-numeric="tabular-nums" testId="die"
      button pointerdown=hold(true) pointerup=hold(false) testId="roll"
        text "Hold to roll"
```

```contract-test
seed 2

test "a hold rolls; the same seed rolls the same"
  expect text "die" == "2"
  tap "roll" drag 0 0 hold 500
  expect state rolls == 10
  expect text "die" == "6"
  clock +500
  expect state rolls == 10
  reload
  expect text "die" == "2"
```

- `stir` is a Lehmer step (modulus 2^31 − 1) whose multiplier depends on the state, then a
  rotation of its 31 bits; both keep 1 … 2^31 − 2 in range. Two stirs scatter the seed and
  three the count, so consecutive counts give unrelated draws.
- A second stream takes a second seed: `roll(time.seed + 1, k, n)`.
- A game's dice, not a secret: that is `crypto.getRandomValues` in a source.
