{"id":20959197,"url":"https://github.com/sanketplus/go-monty-hall","last_synced_at":"2025-07-04T18:10:54.446Z","repository":{"id":94553054,"uuid":"254613098","full_name":"sanketplus/go-monty-hall","owner":"sanketplus","description":"The Proof that thou shalt switch the door!","archived":false,"fork":false,"pushed_at":"2020-04-11T15:23:44.000Z","size":17,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-06T22:28:52.541Z","etag":null,"topics":["monty-hall","monty-hall-problem","monty-hall-simulator","montyhall"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sanketplus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-04-10T11:00:19.000Z","updated_at":"2023-03-04T11:08:39.000Z","dependencies_parsed_at":"2023-07-04T14:16:44.116Z","dependency_job_id":null,"html_url":"https://github.com/sanketplus/go-monty-hall","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/sanketplus/go-monty-hall","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sanketplus%2Fgo-monty-hall","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sanketplus%2Fgo-monty-hall/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sanketplus%2Fgo-monty-hall/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sanketplus%2Fgo-monty-hall/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sanketplus","download_url":"https://codeload.github.com/sanketplus/go-monty-hall/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sanketplus%2Fgo-monty-hall/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263594623,"owners_count":23485877,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["monty-hall","monty-hall-problem","monty-hall-simulator","montyhall"],"created_at":"2024-11-19T01:52:10.168Z","updated_at":"2025-07-04T18:10:54.416Z","avatar_url":"https://github.com/sanketplus.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# go-monty-hall\n\n\u003e Suppose you're on a game show, and you're given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3, which has a goat. He then says to you, \"Do you want to pick door No. 2?\" Is it to your advantage to switch your choice?\n\nThe answer:\n\n## Thou shalt switch the door!\n\nWhile there are mathematical explanations on [Wikipedia](https://en.wikipedia.org/wiki/Monty_Hall_problem), proving it empirically is more satisfying and convincing to me. Before we explain the solution, here are certain assumptions:\n  1. The host must always open a door that was not picked by the contestant.\n  2. The host must always open a door to reveal a goat and never the car.\n  3. The host must always offer the chance to switch between the originally chosen door and the remaining closed door.\n\n## The Experiment:\n\nWe create a function which will take a parameter stating whether we swap our choice (`switch` is a reserved golang keyword :P) We generate two random numbers (less than 3), one is where the prize is and other is our choice.\n\n```golang\nfunc win(swap bool) bool {\n\tr := rand.New(rand.NewSource(time.Now().UnixNano()))\n\tprize := r.Int() % 3\n\tchoice := r.Int() % 3\n    ...\n```\n\nThere are are two possibilities once you choose a door\n  1. You chose correct door\n  2. You chose a door with the goat.\n\n### 1. When you picked the correct door initially\n\n```golang\n        ...\n\tif prize == choice {\n\t\tif swap {\n\t\t\treturn false\n\t\t} else {\n\t\t\treturn true\n\t\t}\n\t}\n    ...\n```\n\nThis is a rather easy one. If we swap on this case, because we chose correct for initially, we lose. If we don't swap, we win.\n\n### 2. You chose a door with the goat\n\nThis is a tricky one. Go back to assumptions 1 and 2 we stated earlier. As per assumption 1, host does not open the door you chose (which is wrong in this case). Out of other two doors, one of which is correct and one is not. According to assumption 2, host will open the door to reveal goat. So the door that remains (apart from one you chose and one the host opened), has the car. Code would look something like this\n\n```golang\n    ...\n    if swap {\n        return true\n    }\n    return false\n}\n```\n\nBecause you chose goat door initially, and host also revealed the goat door, if you swap to the only remaining door, you win. Else, you lose.\n\n## The Trial\n\nWe would run a 100k trials, **with swap**, and see how many time we would win overall.\n\n```golang\nfunc main() {\n\ttrials := 100000\n\tsuccessSwap := 0\n\tswap := true\n\tfor i := 0; i \u003c trials; i++ {\n\t\tif win(swap) {\n\t\t\tsuccessSwap++\n\t\t}\n\t}\n\tfmt.Printf(\"Success with swap: %v is %d%%\",swap, successSwap*100.0/trials)\n}\n```\n\nThe result: we win 66% of times, if we swap.\n```\n$\u003e go build -o play main.go\n$\u003e ./play\nSuccess with swap: true is 66%\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsanketplus%2Fgo-monty-hall","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsanketplus%2Fgo-monty-hall","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsanketplus%2Fgo-monty-hall/lists"}