OneCompiler

Play the Lottery with R

224

To use this script, go to the variables under User Input Variables and edit them as you please.
For the first five values in the "userNums" vector, choose all different numbers from 1 to 69. For
the last value, make it any number from 1 to 26. This last number can be the same as one of the
previous five if you want.

Edit the "money" variable to decide how much you want to spend on the lottery cards. It is already
set to 100 dollars.

Edit the lotCardP to decide the price of the lottery cards. It is already set at 1 dollar.

Edit Jackpot to decide how much you win if you actually get the jackpot. It is already set to
25,000,000.

# Game Variables
won <- lost <- netT <- 0

# User Input Variables
userNums <- c(10,20,30,40,50,6) # Enter your Lottery Number
money <- 100 # Enter the amount (in dollars) you will spend on lottery cards
lotCardP <- 1 # Price of the lottery cards in dollars
jackpot <- 25000000 # Jackpot Value (Amount won if you won the lottery)

# Generates lottery number
lotNumGen <- function(){
  previousNumber <- 0
  fillerVector <- c(0,0,0,0,0,0)
  for (x in 1:6){
    if (x != 6){
      while(T){
        num <- sample(1:69, 1)
        if (!(previousNumber %in% num)){
          fillerVector[x] <- num
          break
        }
      }
    }
    else{
      fillerVector[x] <- sample(1:26, 1)
    }
    previousNumber <- num
  }
  return (fillerVector)
}

# Logic to check if a player has won money
winCheck <- function(vec1, vec2, jackpotVal){
  moneyW <- lastVal <- 0
  sumVal <- 1
  vals <- c(0,0,0,0,0,0)
  sumVals <- c(4,0,3,93,49900,jackpotVal-50000)
  sumVals2 <- c(0,0,7,93,999900)
  for (x in 1:5){
    if (vec1[x] %in% vec2[1:5]){
      vals[x] <- 1
    }
  }
  if (vec1[6] == vec2[6]){
    vals[6] <- 1
  }
  if (vals[6] != 1){
    for (x in 1:5){
      if(vals[x] == 1){
        moneyW <- moneyW + sumVals2[sumVal]
        sumVal <- sumVal + 1
      }
    }
  }
  else{
    for (x in 1:6){
      if(vals[x] == 1){
        moneyW <- moneyW + sumVals[sumVal]
        sumVal <- sumVal + 1
      }
    }
  }
  return (moneyW)
}

# Actual game code
lotNums <- lotNumGen()
lost <- lost - money
if (money/lotCardP == 1){
  won <- won + winCheck(userNums, lotNums, jackpot)
  lotNums <- lotNumGen()
}else{
  for (x in seq(from = 0, to = money, by = lotCardP)){
    won <- won + winCheck(userNums, lotNums, jackpot)
    lotNums <- lotNumGen()
  }
}
netT <- won + lost
print(paste("Amount Won: ", won))
print(paste("Amount Lost: ", lost))
print(paste("Net Gain/Loss: ", netT))