Nifty basketball probability problem

[

basketball simulation

parameters

num_players = 10 num_games = 82 num_shots = 15 num_sims = 50000 prob = .5

create storage for each iteration

results = list() for(i in 1:num_sims){

#create separate matrices for each player
player_matrices = list()
for(j in 1:num_players){

    #create rows for each game in season, column for each shot in game, additional columns
    #to tally whether 10 in a row were hit, 10 in a row missed
    player_matrices[[j]] = matrix(rbinom((num_games*(num_shots+2)),1,prob), num_games, num_shots+2)

    #clear last two columns
    for(a in 1:num_games){
    player_matrices[[j]][a,(num_shots+1)]=0
    player_matrices[[j]][a,(num_shots+2)]=0
    }

    #update second to last column to indicate (yes=1, no=0) whether 10 shots in a row were hit
    #update last column to indicate (yes=1, no=0) whether 10 shots in a row were missed
    for(k in 1:num_games){

        for(l in 1:(num_shots-9)){

        player_matrices[[j]][k,num_shots+1] = ifelse(sum(player_matrices[[j]][k,l:(l+9)])==10, 1, player_matrices[[j]][k,num_shots+1])
        player_matrices[[j]][k,num_shots+2] = ifelse(sum(player_matrices[[j]][k,l:(l+9)])==0, 1, player_matrices[[j]][k,num_shots+2])
        }
    }
}

#store each matrix-season in results
results[[i]] = player_matrices
}

perform calculations

q1: assuming each result is a game, and that only the first game in that list counts for question 1

occurences_q1 = 0 answer_q1 = 0 for(i in 1:num_sims){

counter = 0
for (j in 1:num_players){

    counter = counter + results[[i]][[j]][1,num_shots+1]

}

occurences_q1 = ifelse(counter>0,(occurences_q1+1),occurences_q1)

}

answer_q1 = occurences_q1/(num_sims) answer_q1

[1] 0.03294

q2: assuming each result is a season, and all games count for question 2

occurences_q2 = 0 answer_q2 = c(seq(1,num_sims)) for(i in 1:num_sims){

counter = 0
for (j in 1:num_players){

    counter = counter + sum(results[[i]][[j]][,num_shots+1])
    counter = counter + sum(results[[i]][[j]][,num_shots+2])

}

occurences_q2 = ifelse(counter>0,(occurences_q2+1),occurences_q2)

} answer_q2 = occurences_q2/num_sims answer_q2

[1] 0.9965 ](#sp)

/r/mathriddles Thread