{
  "schemaVersion": 1,
  "slug": "coin-flip-simulator",
  "category": "gaming",
  "kind": "implementation",
  "independentValidation": false,
  "implementationHash": "a04585fef16993412e80f53fb25751e1c816309db81cacb755e6eb322be8976d",
  "provenance": "Generated from the calculation contract used by the published page. Publication is not external validation.",
  "sourceFiles": [
    "data/calculators/coin-flip-simulator.json",
    "src/engine.js"
  ],
  "runtimeSource": "/sources/engine.txt",
  "runtimeHash": "5c888d8898afbdee36267bb9977829e1e2ac568f6677c8ee87469c139864645c",
  "scope": "The compute function records implemented equations, constants, lookup tables and custom rules. Inputs record defaults and allowed values; outputs record precision and units. Defaults are examples, not recommendations. General background references are not evidence for this specific algorithm.",
  "publishedFormula": {
    "formula": "bestSide = side when runLength > longestRun",
    "vars": [
      [
        "U_i",
        "pseudo-random number in [0,1)"
      ],
      [
        "H",
        "heads (outcome symbol)"
      ],
      [
        "X_i",
        "1 for heads, 0 for tails"
      ],
      [
        "n",
        "number of flips"
      ],
      [
        "h",
        "total heads"
      ],
      [
        "P_h",
        "heads percentage"
      ]
    ],
    "note": "The side recorded is the one in the longest run; tied runs keep the first. The seeded generator is deterministic."
  },
  "externalEvidence": null,
  "contract": {
    "compute": "const rng=s=>{let a=Math.floor(s)>>>0;return()=>{a=(a+0x6D2B79F5)>>>0;let t=a;t=Math.imul(t^(t>>>15),t|1);t^=t+Math.imul(t^(t>>>7),t|61);return((t^(t>>>14))>>>0)/4294967296;};};const r=rng(seed);let h=0,t=0,cur=0,best=0,side='H',bestSide='H',seq='';for(let i=0;i<flips;i++){const v=r()<0.5;if(v){h++;if(seq.length<20)seq+='H';}else{t++;if(seq.length<20)seq+='T';}if(i===0){cur=1;side=v?'H':'T';}else if((v&&side==='H')||(!v&&side==='T'))cur++;else{cur=1;side=v?'H':'T';}if(cur>best){best=cur;bestSide=side;}}return{heads:h,tails:t,headsPct:h/flips*100,tailsPct:t/flips*100,longestStreak:best,streakSide:bestSide==='H'?'Heads':'Tails',sequence:seq};",
    "inputs": [
      {
        "name": "flips",
        "type": "number",
        "default": 100,
        "min": 1,
        "max": 100000,
        "step": 1
      },
      {
        "name": "seed",
        "type": "number",
        "default": 42,
        "min": 0,
        "max": 1000000,
        "step": 1,
        "advanced": true
      }
    ],
    "outputs": [
      {
        "name": "heads",
        "precision": 0,
        "unit": ""
      },
      {
        "name": "tails",
        "precision": 0,
        "unit": ""
      },
      {
        "name": "headsPct",
        "precision": 1,
        "unit": "%"
      },
      {
        "name": "tailsPct",
        "precision": 1,
        "unit": "%"
      },
      {
        "name": "longestStreak",
        "precision": 0,
        "unit": ""
      },
      {
        "name": "streakSide",
        "precision": 0,
        "unit": "",
        "type": "text"
      },
      {
        "name": "sequence",
        "precision": 0,
        "unit": "",
        "type": "text"
      }
    ],
    "interpret": null
  }
}
