-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmontyhall.rb
More file actions
73 lines (58 loc) · 1.67 KB
/
montyhall.rb
File metadata and controls
73 lines (58 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#Modelling the Monty Hall Problem
#A little user interface
puts "Welcome to the Monty Hall Problem!"
puts "You pick one out of three doors at random."
puts "Behind two doors, there are goats."
puts "Behind 1, $64,000."
puts "You pick one door at random."
puts "One door is opened to reveal a goat."
puts "Do you want to stay with your original random choice\[1\] or pick the unrevealed random door\[2\]?"
puts "Please enter 1 or 2."
puts "We will run this simulation 1000 times."
door_switching_strategy = gets.chomp
sum = 0
switch_choice = 0
1000000.times do
#Assigning values to the array
array = [false, false, false]
array[rand(3)] = true
#"Picking a door"
random_choice = rand(3)
#Opening a door with a goat
open_goat_door_array_index = rand(3)
while array[open_goat_door_array_index]
open_goat_door_array_index = rand(3)
end
#Simplifying later code with spaghetti logic, refactor?
if random_choice == 0 && open_goat_door_array_index == 1
switch_choice = 2
end
if random_choice == 0 && open_goat_door_array_index == 2
switch_choice = 1
end
if random_choice == 1 && open_goat_door_array_index == 0
switch_choice = 2
end
if random_choice == 1 && open_goat_door_array_index == 2
switch_choice = 0
end
if random_choice == 2 && open_goat_door_array_index == 0
switch_choice = 1
end
if random_choice == 2 && open_goat_door_array_index == 1
switch_choice = 0
end
#Taking into account player choice and adding to the accumulator
if door_switching_strategy == '1'
#staying with the original door choice
if array[random_choice]
sum += 1
end
else
#changing door choice
if array[switch_choice]
sum += 1
end
end
end
puts "Won #{sum} times"