This RMarkdown file is intended to lay out the logic of a mobile app designed for those addicted to the lottery. By showing a user how to calculate the incredibly small probabilities of winning the lottery, we hope that the app will help them better grasp that buying multiple lottery tickets will do little to help them win. Through this understanding, they will hopefully stop purchasing lottery tickets in an unhealthy manner.
factorial <- function(n) {
product = 1
for (i in 1:n) {
product = product * i
}
return(product)
}
combinations <- function(n, k) {
numerator <- factorial(n)
denominator <- factorial(k) * factorial(n - k)
return(numerator / denominator)
}
one_ticket_probability <- function(nums) {
total_combinations <- combinations(49, 6)
prob <- (1 / total_combinations) * 100
pretty_prob <- sprintf("%1.9f", prob)
s <- paste("You have a ", pretty_prob, "% chance of winning the big prize.", sep = "")
return(s)
}
one_ticket_probability(c(1, 2, 3, 4, 5, 6))
## [1] "You have a 0.000007151% chance of winning the big prize."
library(tidyverse)
lottery649 <- read_csv("649.csv")
print(dim(lottery649))
## [1] 3665 11
head(lottery649, 3)
## # A tibble: 3 x 11
## PRODUCT `DRAW NUMBER` `SEQUENCE NUMBE~ `DRAW DATE` `NUMBER DRAWN 1`
## <dbl> <dbl> <dbl> <chr> <dbl>
## 1 649 1 0 6/12/1982 3
## 2 649 2 0 6/19/1982 8
## 3 649 3 0 6/26/1982 1
## # ... with 6 more variables: `NUMBER DRAWN 2` <dbl>, `NUMBER DRAWN 3` <dbl>,
## # `NUMBER DRAWN 4` <dbl>, `NUMBER DRAWN 5` <dbl>, `NUMBER DRAWN 6` <dbl>,
## # `BONUS NUMBER` <dbl>
tail(lottery649, 3)
## # A tibble: 3 x 11
## PRODUCT `DRAW NUMBER` `SEQUENCE NUMBE~ `DRAW DATE` `NUMBER DRAWN 1`
## <dbl> <dbl> <dbl> <chr> <dbl>
## 1 649 3589 0 6/13/2018 6
## 2 649 3590 0 6/16/2018 2
## 3 649 3591 0 6/20/2018 14
## # ... with 6 more variables: `NUMBER DRAWN 2` <dbl>, `NUMBER DRAWN 3` <dbl>,
## # `NUMBER DRAWN 4` <dbl>, `NUMBER DRAWN 5` <dbl>, `NUMBER DRAWN 6` <dbl>,
## # `BONUS NUMBER` <dbl>
data1 <- c(1, 3, 5)
data2 <- c(2, 4 ,6)
data3 <- c(8, 9, 7)
## Answer
unnamed_list <- list(data1, data2, data3)
first_vector <- unnamed_list[[1]]
named_list <- list(first = data1, second = data2, third = data3)
first_item_sum <- named_list$data[1] + named_list$data2[1] + named_list$data3[1]
data1 <- c(1, 3, 5)
data2 <- c(2, 4, 6)
data3 <- c(8, 9, 7)
data_list <- list(data1, data2, data3)
## Answer
averages <- pmap(data_list, function(x, y, z) { (x + y + z) / 3 })
first_average <- unlist(averages)[1]
historical_lots <- pmap(
list(
u <- lottery649$`NUMBER DRAWN 1`,
v <- lottery649$`NUMBER DRAWN 2`,
w <- lottery649$`NUMBER DRAWN 3`,
x <- lottery649$`NUMBER DRAWN 4`,
y <- lottery649$`NUMBER DRAWN 5`,
z <- lottery649$`NUMBER DRAWN 6`
),
.f <- function(u, v, w, x, y, z) { c(u, v, w, x, y, z) }
)
library(sets)
## Registered S3 method overwritten by 'sets':
## method from
## print.element ggplot2
##
## Attaching package: 'sets'
## The following object is masked from 'package:forcats':
##
## %>%
## The following object is masked from 'package:stringr':
##
## %>%
## The following object is masked from 'package:dplyr':
##
## %>%
## The following object is masked from 'package:purrr':
##
## %>%
## The following object is masked from 'package:tidyr':
##
## %>%
## The following object is masked from 'package:tibble':
##
## %>%
check_historical_occurrences <- function(lot, hist_lots = historical_lots) {
historical_matches <- map(hist_lots, function(x) {setequal(x, lot)})
num_past_matches <- sum(unlist(historical_matches))
s <- paste("The combination you entered has appeared ",
num_past_matches,
" times in the past. ",
"Your chance of winning the big prize in the next drawing using this combination is 0.0000072%", sep = "")
return(s)
}
check_historical_occurrences(c(3, 12, 11, 14, 41, 43))
## [1] "The combination you entered has appeared 1 times in the past. Your chance of winning the big prize in the next drawing using this combination is 0.0000072%"
check_historical_occurrences(c(1, 2, 3, 4, 5, 6))
## [1] "The combination you entered has appeared 0 times in the past. Your chance of winning the big prize in the next drawing using this combination is 0.0000072%"
multi_ticket_probability <- function(n) {
total_combinations <- combinations(49, 6)
prob <- (n / total_combinations) * 100
pretty_prob <- sprintf("%1.9f", prob)
s <- paste("you have a ", pretty_prob, "% chance of winning the big prize.", sep = "")
return(s)
}
test_amounts <- c(1, 10, 100, 1000000, 6991908, 13983816)
for (n in test_amounts) {
print(paste("For ", n, " tickets, ", multi_ticket_probability(n), sep =))
}
## [1] "For 1 tickets, you have a 0.000007151% chance of winning the big prize."
## [1] "For 10 tickets, you have a 0.000071511% chance of winning the big prize."
## [1] "For 100 tickets, you have a 0.000715112% chance of winning the big prize."
## [1] "For 1e+06 tickets, you have a 7.151123842% chance of winning the big prize."
## [1] "For 6991908 tickets, you have a 50.000000000% chance of winning the big prize."
## [1] "For 13983816 tickets, you have a 100.000000000% chance of winning the big prize."
probability_less_6 <- function(n) {
n_combinations_ticket = combinations(6, n)
n_combinations_remaining = combinations(49 - n, 6 - n)
successful_outcomes = n_combinations_ticket * n_combinations_remaining
n_combinations_total = combinations(49, 6)
prob = (successful_outcomes / n_combinations_total) * 100
pretty_prob <- sprintf("%1.9f", prob)
s <- paste("you have a ", pretty_prob, "% chance of winning the big prize.", sep = "")
return(s)
}
winning_nums <- c(3, 4, 5)
for (n in winning_nums) {
print(paste("For ", n, " tickets, ", probability_less_6(n), sep = ""))
}
## [1] "For 3 tickets, you have a 2.171081198% chance of winning the big prize."
## [1] "For 4 tickets, you have a 0.106194189% chance of winning the big prize."
## [1] "For 5 tickets, you have a 0.001887897% chance of winning the big prize."