# Seeded Reproducibility

SimHealth is built around mathematical determinism. Every synthetic record draw is governed by a 64-bit unsigned integer seed. Given the same dictionary definition version and the same seed, SimHealth produces byte-for-byte identical output across every execution environment.

> [!NOTE]
> Deterministic generation eliminates flaky test suites and stochastic CI runs. A bug reproduced once with a seed can be replayed indefinitely on any developer machine or staging cluster.

## How determinism works

SimHealth uses a high-performance pseudo-random number generator (PRNG) with deterministic state derivation. When you request a draw with `--seed 1842`:

1. **Seed Initialization**: The master seed initializes the root generator.
2. **Topological Traversal**: The compilation DAG visits parent entities before dependent entities.
3. **Deterministic Splitting**: For child records and nested fragments, the generator splits its entropy stream deterministically using the field path and entity index.
4. **Relational Consistency**: Foreign keys and shared attributes maintain identical values across all related tables in the sample.

```bash
# Draw 1: Maria Santos
simhealth sample person --count 1 --seed 1842

# Draw 2: Identical byte-for-byte replay
simhealth sample person --count 1 --seed 1842
```

## Seed branching and relational coherence

In relational schemas (such as a patient with multiple encounters, diagnoses, and lab results), SimHealth does not reuse random values randomly. Instead, it computes an ancestral state tree:

```text
Master Seed (1842)
  ├── person[0] (Seed Derived: 0x9A4F...)
  │     ├── mrn: MRN-004928
  │     ├── first_name: Maria
  │     └── encounter[0] (Seed Derived: 0x3E11...)
  │           ├── encounter_id: ENC-10928
  │           └── condition: Type 2 Diabetes
  └── person[1] (Seed Derived: 0x7B2C...)
```

Even when generating nested arrays of varying length, the draw for `encounter[0]` does not disrupt the entropy sequence for subsequent top-level records.

## Multi-language code examples

### Generate with explicit seed

```tabs
=== SimHealth CLI
```bash
simhealth sample person --count 10 --seed 42001 --format json
```
=== cURL
```bash
curl -s -H "Authorization: Bearer $SIMHEALTH_API_KEY" \
  "https://api.preprod.simhealth.interoperabilitypro.com/v1/projects/{project_id}/random/person?count=10&seed=42001"
```
=== Python
```python
import requests

response = requests.get(
    "https://api.simdata.interoperabilitypro.com/v1/projects/default/random/person",
    headers={"Authorization": "Bearer sim_live_abcdef123456"},
    params={"count": 10, "seed": 42001},
)
records = response.json()["records"]
```
=== TypeScript
```typescript
const response = await fetch(
  "https://api.simdata.interoperabilitypro.com/v1/projects/default/random/person?count=10&seed=42001",
  {
    headers: { Authorization: "Bearer sim_live_abcdef123456" },
  }
);
const data = await response.json();
```
```

> [!TIP]
> When testing boundary conditions or negative data scenarios, store the seed in your test fixture or git repository. If an edge case fails, commit the failing seed to prevent regressions.

## Seed invalidation rules

A seed produces identical records as long as the underlying dictionary schema remains unchanged. The following events generate a new dictionary revision:

- Modifying members in a referenced `Set`
- Altering the shape or field order in a `Record`
- Changing probability boundaries in a `Distribution`
- Publishing a batch update via `POST /v1/projects/{project_id}/catalog/batch`

Existing seeds evaluated against the updated dictionary version will sample from the new state space deterministically.
