library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)

set.seed(905)

Question 7: Another form of bet for roulette is to bet that a specific number (say 17) will turn up. If the ball stops on your number, you get your dollar back plus 35 dollars. If not, you lose your dollar. Write a program that will plot your winnings when you make 500 plays of roulette at Las Vegas, first when you bet each time on red (see Exercise 6), and then for a second visit to Las Vegas when you make 500 plays betting each time on the number 17. What differences do you see in the graphs of your winnings on these two occasions?

My first visit

In this visit, I’m taking the more conservative roulette route, where I can double my bet by landing on my color of choice (red). Intuitively, I know this is roughly (key word!) a 50/50 shot. Let’s see what happens when I play a bunch.

#creating list of roulette rounds
roulette_results1 <- sample(1:38,size=500,replace=TRUE)

#just for fun I'll use the actual red numbers in roulette, treating 37 and 38 and 0 and 00 respectively.
red_wins <- c(1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36)

#making it into a dataframe
roulette_df1 <- data.frame(roulette_results1)
names(roulette_df1)[1] <- "roulette_results"

#setting the condition: +1 if I won, -1 if I lost.
roulette_df1$net_dollars <- ifelse(roulette_df1$roulette_results %in% red_wins, 
                                       1, -1)

#creating a cumulative sum, AKA my running winnings tally.
roulette_df1$cumsum_dollars <- cumsum(roulette_df1$net_dollars)

roulette_df1 %>%
  head(10)
##    roulette_results net_dollars cumsum_dollars
## 1                14           1              1
## 2                13          -1              0
## 3                29          -1             -1
## 4                33          -1             -2
## 5                35          -1             -3
## 6                31          -1             -4
## 7                12           1             -3
## 8                33          -1             -4
## 9                10          -1             -5
## 10               30           1             -4

Now let’s visualize my winnings and see how I did!

ggplot(roulette_df1, aes(x = seq_along(cumsum_dollars), y = cumsum_dollars)) + 
  geom_line() + 
  xlab('Roulette Tries') + 
  ylab('Net $') + 
  ggtitle('My Net "Winnings" in Roulette (Betting on Red)')

I mean, I went to Vegas and only lost 7 or 8 dollars. I’ll chalk that up as a win.

My second visit

Guess I was feeling lucky!

In order to solve the second part of this question, I’ll set my “lucky number” of 17 statically, then run 500 trials of roulette. Every time the result matches my number, I’ll set the net winnings at 35 dollars. When the numbers don’t match, it’s -1 dollars (the dollar I put down). I’ll make a table of the results, and create a cumulative sum column to tally my running results.

#The number the roullette results must match in order for me to win
#In my index, I'm treating 37 as '0' and 38 as '00'
my_lucky_number <- 17

#creating list of roulette rounds
roulette_results2 <- sample(1:38,size=500,replace=TRUE)

#making it into a dataframe
roulette_df2 <- data.frame(roulette_results2)
names(roulette_df2)[1] <- "roulette_results"

#setting the condition: +35 if I won, -1 if I lost.
roulette_df2$net_dollars <- ifelse(roulette_df2$roulette_results == my_lucky_number, 
                                       35, -1)

#creating a cumulative sum, AKA my running winnings tally.
roulette_df2$cumsum_dollars <- cumsum(roulette_df2$net_dollars)

roulette_df2 %>%
  head(10)
##    roulette_results net_dollars cumsum_dollars
## 1                 1          -1             -1
## 2                 4          -1             -2
## 3                20          -1             -3
## 4                12          -1             -4
## 5                19          -1             -5
## 6                14          -1             -6
## 7                16          -1             -7
## 8                22          -1             -8
## 9                26          -1             -9
## 10               18          -1            -10

Let’s use ggplot to see how I came out in the end:

ggplot(roulette_df2, aes(x = seq_along(cumsum_dollars), y = cumsum_dollars)) + 
  geom_line() + 
  xlab('Roulette Tries') + 
  ylab('Net $') + 
  ggtitle('My Net "Winnings" in Roulette (Betting on 17)')

Unbelievably, I appear to have won a couple dollars! I can’t imagine this is a likely result, and we can see how wildly variable the cumulative sum is compared to betting on red. I’m glad I stopped at 500 because we could have been starting a worrying trend there.

As we can see, it’s certainly not impossible to win money with either method. But based on these results it seems the risk of betting on individual numbers comparatively high. Even though the potential winnings from landing on the right number are much higher than winning by color, we can see how much an overall positive trend depends on frequent, somewhat unlikely hits.