# Randomization & Counterbalancing, TreasureHuntForTwo Everything is **deterministic from the participant ID**, the ID *is* the order (one "order file" per ID, like the original task). Re-running an ID reproduces the exact same session. All of it is generated by `orders.make_order(pid)` (pure numpy, fully testable). The companion notebook **`randomization.ipynb`** generates these for a whole sample and checks the balance. **`make_order_files.py`** additionally *writes* a real, human-readable order file per participant to `orders/order_.csv`, a durable, code-independent record (`--verify` re-checks any file still matches what `orders.py` computes right now). ## Two ways a session gets its design **From the pid** (the lab sample, the SONA pilot, everything collected so far). The pid *is* the order, as described below. **From the pool** (`randos.py`, used by BEAR). The link carries `?rando=`, and the design is a function of that number alone. The pid falls back to being a label and a folder name: it can be any number, in any order, with any gaps, and two people can share one. Nothing about the rotations changes, only where the counter comes from. A session with no `?rando=` takes the pid route, so no collected session is affected. ### Why the pool exists The rotations below are only even when `number` behaves like a counter. A recruitment identifier does not. The SONA pilot is the demonstration: its pids are HMAC hashes, so its numbers are arbitrary, and its rotations came out balanced in expectation instead of by construction. BEAR adds two more cases the pid cannot cover: a parent and child who share one pid and need two designs, and two siblings who need two designs of their own. Periods are 12 (block order), 24 (chest slots) and 5 (layout). 12 divides 24, so a design is fixed by `(n mod 24, n mod 5)`, which repeats every 120. `randos.POOL_SIZE` is 206, which is `2 * PARENT_GAP`: children take ids 1..C and parents 1+GAP..C+GAP, so the two blocks collide once C passes the gap and 103 dyads is the ceiling whatever the pool holds. Measured min and max uses of each cell over candidate id sets: | id set | block /12 | chest /24 | layout /5 | |---|---|---|---| | 1..120 consecutive | 10-10 | 5-5 | 24-24 | | 1..100 consecutive | 8-9 | 4-5 | 20-20 | | every 7th | 8-9 | 4-5 | 20-20 | | 100 ids picked at random | 5-12 | 0-7 | 15-27 | | every 10th | 0-17 | 0-9 | 0-100 | Consecutive is not the requirement; a constant step coprime to 24 and 5 is just as even. A step sharing a factor collapses, which is why every 10th puts all 100 sessions on one layout. Step 1 is coprime to everything, so it is the one that cannot be got wrong. Children take ids 1..N and parents `randos.PARENT_GAP` (103) above them. The gap is coprime to 24 and 5, so a parent moves off their child on all three rotations at once; 100 would leave every dyad on one layout and 120 would give every dyad the identical design. ## The ID ``, e.g. `0003` = group 0 (control), participant 3; `1007` = group 1 (CU), #7. The seed is `group·1,000,003 + number + 1`, so every participant gets an independent stream. IDs are assigned **strictly sequentially per group** (`next_id.py` tells you the next one), this matters because two of the counterbalanced dimensions below are systematic rotations over `number`, not a random draw, so skipping/reusing numbers breaks their balance guarantee. ## What is randomized (and how) | Thing | How | Counterbalanced? | |------|-----|------------------| | **Chest appearance** | all 24 chests used, **one per (house, side) slot**, never reused this session | **systematically counterbalanced**, each letter (A-D) owns a fixed 6-slot share of the 24 chests, rotated by `number`; every chest lands under every letter exactly 6 times per 24 consecutive IDs (within a group), so no colour is systematically tied to any reward profile, even for a partial sample. No persistent look-to-profile identity *within* a session either, so a colour can't be used as an "always pick this one" shortcut (see below) | | **Block order** (the experienced sequence of pairs) | a **cyclic Latin square** over `number` (row = each of the 12 pair-slots shifted by `number`); the **Nth house entered = the Nth pair** | **systematically counterbalanced**, every pair-slot lands at every entry-position exactly once per 12 consecutive IDs (within a group), so balance holds even if some participants quit partway | | **House location** | one of **5 fixed layouts**, chosen by `number % 5` | **systematically counterbalanced** (≈ equal N per layout) | | **House appearance** | a permutation of 12 house types per ID | randomised (decorative; not experimental) | | **Side (left/right)** each trial | a per-block, per-trial 0/1 schedule | randomised per ID | | **Outcomes** | exact **12 gain / 2 loss / 2 neutral** per option, per recipient, per block (±10 / 0) | exact counts (not probabilistic) | | **Coin-float order** | per block: **6 you-first + 6 partner-first** | balanced within participant (6/6), shuffled per ID | | **Partner ID** | a deterministic 4-digit "community-database" code (cover story only) | n/a | | **Practice** | fixed structure (1 forest hut, AB pair, 6 trials); sides + outcome order seeded per ID | n/a | ### Why chest appearance changed from "4 looks, reused" to "24 looks, never reused" The original PsychoPy task drew fresh stimuli each block, no chest look ever carried a persistent identity. An earlier NiceWebRL-port deviation gave each reward profile (A–D) ONE fixed look reused across every house that profile appeared in. Pilot testers found this exploitable ("I always pick red because it's always been the best"), a real deviation from the original design, not just a display preference. The fix restores the original property: every one of the 24 chests in a session (12 houses × 2 chests) is unique and never repeats, so appearance carries no information about reward valence across houses. Only PRACTICE keeps a persistent look-per-letter (4 named chests reused across its scripted steps), by design, the tutorial is a fixed script, not the free-choice main task. ### Why chest-colour balance changed from "fresh random draw" to "cyclic rotation" This mirrors the block-order rationale below exactly. A fresh `permutation(24)` per participant is unbiased *in expectation*, but only balances out as the sample grows, real-world attrition can leave whatever prefix of the sample *did* complete skewed. Each letter now owns a fixed baseline of 6 stimulus "slots" out of 24 (A: 0-5, B: 6-11, C: 12-17, D: 18-23), and every slot gets rotated by the same amount (`number`). A rotated permutation is still a permutation, so a session still uses each of the 24 chests exactly once, but because every slot shares one rotation, every chest is guaranteed to land under every letter exactly 6 times per complete 24-ID cycle (within a group), not just approximately. `orders.py`'s self-test checks this directly; `next_id.py`'s colour-vs-valence report now confirms an exact guarantee rather than hoping a random draw stays roughly even. ### Why block order changed from "random shuffle" to "cyclic Latin square" A per-ID random shuffle only balances entry-position in expectation, as N grows, real-world attrition (SONA no-shows, quits) can leave whatever *did* complete skewed. A cyclic Latin square (row `number` = the base order rotated by `number`) guarantees position balance for *any completed prefix* of 12 consecutive IDs within a group, not just after every last participant finishes. This mirrors `house_pos`'s existing `number % 5` rotation, extended to block order. ## The key design point, block order is independent of location In this free-roam world the player walks to houses in whatever order they like. The **pair / condition** a house holds is **not** tied to its location, it's set by the **order the house is entered**: the 1st house entered is block 0 (gets `block_order[0]`), the 2nd is block 1, etc. So two participants can have the same block order experienced through completely different physical houses/locations. This reproduces the original task's block-order counterbalancing while keeping the explorable map. In the data: `blockIndex` = the experienced block (0–11); `physical_place` / `physical_house` record *where* it happened. The summary's `house_by_block` ties them together. ## Reward profiles (fixed) | Option | self | partner | |--------|------|---------| | A | good | good | | B | good | bad | | C | bad | good | | D | bad | bad | "good" = 12 gain / 2 loss / 2 neutral over 16 trials; "bad" = the reverse. A gain shows +10 coins, a loss −10, neutral 0. Money = coins ÷ 100, capped $6, paid to both you and the partner. ## Reproduce / audit ```bash uv run python orders.py # self-tests + sample manifest rows uv run python randomization.ipynb # (open in Jupyter) full sample + balance checks uv run python next_id.py # next ID to assign per group + colour/valence balance uv run python make_order_files.py # write orders/order_.csv for the target sample uv run python make_order_files.py --verify # check existing order files still match orders.py ``` `orders.make_order(pid)` returns the full design; `orders.manifest_row(pid)` is the flat one-row summary that also lands in `randomization_log.csv` on GitLab. > ⚠️ **Freeze `orders.py` once real data collection starts**, the randomisation is derived > from the ID, so any change alters every participant's design.