Browse Source

Init commit

master
balhau@balhau.net 11 months ago
commit
1ba9358071
No known key found for this signature in database GPG Key ID: 1E666F326A121830
4 changed files with 177 additions and 0 deletions
  1. +7
    -0
      Readme.md
  2. +3
    -0
      go.mod
  3. +90
    -0
      hall/montystate.go
  4. +77
    -0
      montyhall.go

+ 7
- 0
Readme.md View File

@@ -0,0 +1,7 @@
# GO Tinker

## What?

This is a repository to serve as kindergarten for golang applications. As of now this is almost empty. Only a small simulation of [Monty Hall Problem](https://en.wikipedia.org/wiki/Monty_Hall_problem).

In time more examples will be added here

+ 3
- 0
go.mod View File

@@ -0,0 +1,3 @@
module balhau.net/tinker

go 1.14

+ 90
- 0
hall/montystate.go View File

@@ -0,0 +1,90 @@
package hall

import (
"fmt"
"math/rand"
"time"
)

type MontyDoor int

type Player interface {
SelectDoor()
ChangeDoor(slot MontyDoor)
GetDoor() MontyDoor
}

type Presenter interface {
OpenDoor(slot MontyDoor) MontyDoor
}

type MontyHall struct {
solution MontyDoor
}

type ConservativePlayer struct {
solution MontyDoor
}

type ChangerPlayer struct {
solution MontyDoor
}

func RandomDoor() MontyDoor {
rand.Seed(time.Now().UnixNano())
return MontyDoor(rand.Intn(3) + 1)
}

func (montyHall *MontyHall) OpenDoor(slot MontyDoor) MontyDoor {
alternative := RandomDoor()
loop := true
for ok := true; ok; ok = loop {
alternative = RandomDoor()
loop = (alternative == montyHall.solution || alternative == slot)
}
return alternative
}

func (referee *MontyHall) SelectDoor() {
referee.solution = RandomDoor()
}

func (player *ConservativePlayer) SelectDoor() {
player.solution = RandomDoor()
}

func (player *ChangerPlayer) SelectDoor() {
player.solution = RandomDoor()
}

func (montyHall *MontyHall) ChangeDoor(sol MontyDoor) {
//Does nothing
}

func (player *ConservativePlayer) ChangeDoor(sol MontyDoor) {
//Does nothing
}

func (player *ChangerPlayer) ChangeDoor(sol MontyDoor) {
alternative := RandomDoor()
for ok := true; ok; ok = alternative == player.solution || alternative == sol {
alternative = RandomDoor()
}
player.solution = alternative
}

func (montyHall *MontyHall) GetDoor() MontyDoor {
return montyHall.solution
}

func (player *ConservativePlayer) GetDoor() MontyDoor {
return player.solution
}

func (player *ChangerPlayer) GetDoor() MontyDoor {
return player.solution
}

func (montyHall *MontyHall) String() string {
return fmt.Sprintf("[Referee %d]", montyHall.solution)
}

+ 77
- 0
montyhall.go View File

@@ -0,0 +1,77 @@
package main

import (
"fmt"

"balhau.net/tinker/hall"
)

func main() {

iterations := 1000

type SolutionCounter struct {
Conservative int
ChangerBefore int
ChangerAfter int
}

counter := SolutionCounter{0, 0, 0}

for i := 0; i < iterations; i++ {
montyOne := hall.MontyHall{}
montyTwo := hall.MontyHall{}

conservative := hall.ConservativePlayer{}
changer := hall.ChangerPlayer{}

montyOne.SelectDoor()
conservative.SelectDoor()

montyTwo.SelectDoor()
changer.SelectDoor()

changerBefore := changer.GetDoor()
conservativeBefore := conservative.GetDoor()

openPortConservative := montyOne.OpenDoor(conservativeBefore)
openPortChanger := montyTwo.OpenDoor(changerBefore)

conservative.ChangeDoor(openPortConservative)
changer.ChangeDoor(openPortChanger)

if conservative.GetDoor() == montyOne.GetDoor() {
counter.Conservative += 1
}

if changer.GetDoor() == montyTwo.GetDoor() {
counter.ChangerAfter += 1
}

if changerBefore == montyTwo.GetDoor() {
counter.ChangerBefore += 1
}

conservSol := "x"
changerSol := "x"

if conservative.GetDoor() == montyOne.GetDoor() {
conservSol = "o"
}

if changer.GetDoor() == montyTwo.GetDoor() {
changerSol = "o"
}

fmt.Println(fmt.Sprintf("[Conservative: s=%d,p=%d,b=%d,a=%d] -- %s", montyOne.GetDoor(), openPortConservative, conservativeBefore, conservative.GetDoor(), conservSol))
fmt.Println(fmt.Sprintf("[Changer: s=%d,p=%d,b=%d,a=%d] -- %s", montyTwo.GetDoor(), openPortChanger, changerBefore, changer.GetDoor(), changerSol))
fmt.Println()
}

conservative_accuracy := (float32(counter.Conservative) / float32(iterations)) * 100
changer_before_accuracy := (float32(counter.ChangerBefore) / float32(iterations)) * 100
changer_after_accuracy := (float32(counter.ChangerAfter) / float32(iterations)) * 100

fmt.Printf(fmt.Sprintf("[Conservative: %d/%f, ChangerBefore: %d/%f, ChangerAfter: %d/%f]\n", counter.Conservative, conservative_accuracy, counter.ChangerBefore, changer_before_accuracy, counter.ChangerAfter, changer_after_accuracy))

}

Loading…
Cancel
Save