Guest User

Untitled

a guest
Dec 12th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "math/rand"
  6. )
  7.  
  8. // Possible entries
  9. const Car = 0
  10. const Goat = 1
  11.  
  12. // Possible results
  13. const ChoiceWin = 1
  14. const ChoiceLose = 2
  15. const ChoiceInvalid = 3
  16.  
  17. /*
  18. * Create int[3] with two Goats and one Car
  19. * @returns []int
  20. */
  21. func makeMonty() []int {
  22. monty := []int{
  23. Car,
  24. Car,
  25. Car,
  26. }
  27.  
  28. // Attempt to randomize two indexes
  29. goatIdx1 := rand.Intn(len(monty))
  30. goatIdx2 := rand.Intn(len(monty))
  31.  
  32. // It's possible to have two of the same so keep going until it works
  33. for goatIdx2 == goatIdx1 {
  34. goatIdx2 = rand.Intn(len(monty))
  35. }
  36.  
  37. // Make ze goat
  38. monty[goatIdx1] = Goat
  39. monty[goatIdx2] = Goat
  40.  
  41. return monty
  42. }
  43.  
  44. /*
  45. * Randomly "reveal" first item of monty and return index and value
  46. */
  47. func reveal(choice int, monty []int) (int, int) {
  48. revealIdx := choice
  49. for revealIdx == choice {
  50. revealIdx = rand.Intn(len(monty))
  51. }
  52.  
  53. return revealIdx, monty[revealIdx]
  54. }
  55.  
  56. /*
  57. * Make choice and stick to it (statistically wrong)
  58. */
  59. func chooseWrong(monty []int) int {
  60. choice := rand.Intn(len(monty))
  61. _, revealVal := reveal(choice, monty)
  62.  
  63. // If revealed tile is the winner we wont reach the puzzle
  64. if revealVal != Car {
  65. if monty[choice] == Car {
  66. return ChoiceWin
  67. } else {
  68. return ChoiceLose
  69. }
  70. }
  71.  
  72. return ChoiceInvalid
  73. }
  74.  
  75. /*
  76. * Make choice and change (statistically right)
  77. */
  78. func chooseRight(monty []int) int {
  79. choice := rand.Intn(len(monty))
  80. revealIdx, revealVal := reveal(choice, monty)
  81.  
  82. if revealVal != Car {
  83. choiceNew := 0
  84. for (choiceNew == choice || choiceNew == revealIdx) {
  85. choiceNew++
  86. }
  87.  
  88. if monty[choiceNew] == Car {
  89. return ChoiceWin
  90. } else {
  91. return ChoiceLose
  92. }
  93. }
  94.  
  95. return ChoiceInvalid
  96. }
  97.  
  98. /*
  99. * Calls $fn $count times and output results as formatted string to $ch
  100. */
  101. func chooseGo(ch chan string, count int, name string, fn func([]int) int) {
  102. winCount := 0
  103. lossCount := 0
  104. validCount := 0
  105.  
  106. for i := 0; i < count; i++ {
  107. ret := fn(makeMonty())
  108.  
  109. if (ret == ChoiceWin) {
  110. winCount++
  111. validCount++
  112. } else if (ret == ChoiceLose) {
  113. lossCount++
  114. validCount++
  115. }
  116. }
  117.  
  118. // Whoa
  119. ch <-
  120. fmt.Sprintln(name) +
  121. fmt.Sprintln(" win%: ", float64(winCount) / float64(validCount)) +
  122. fmt.Sprintln(" wins: ", winCount) +
  123. fmt.Sprintln(" losses: ", lossCount) +
  124. fmt.Sprintln(" total: ", validCount)
  125. }
  126.  
  127. func main() {
  128. ch := make(chan string)
  129. count := 5000000 // big number
  130.  
  131. go chooseGo(ch, count, "wrong", chooseWrong)
  132. go chooseGo(ch, count, "right", chooseRight)
  133.  
  134. message := <- ch
  135. fmt.Println(message)
  136.  
  137. message = <- ch
  138. fmt.Println(message)
  139. }
Add Comment
Please, Sign In to add comment