So I'm trying to get all the possible combinations of rolling two dice n
number of times.
Currently I have:
# Get all possible rolls with two dice
d <- list(1, 2, 3, 4, 5, 6)
rolls <- lapply(apply(expand.grid(d, d), 1, identity), unlist)
# Get all possible ways to roll two dice twice
trials <-lapply(apply(expand.grid(rolls, rolls), 1, identity), unlist)
d
stores all possible values you can get on a single dice. rolls
stores all possible outcomes of rolling two dice at the same time. And trials
stores all possible outcomes of rolling two dice at the same time, twice in a row.
I can modify the last line as
trials <-lapply(apply(expand.grid(rolls, rolls, rolls), 1, identity), unlist)
to get all possible outcomes of rolling two dice at the same time, three times in a row, but I cannot figure out how to make the number of times variable, so that I could pass some arbitrary number n
and get all possible outcomes of rolling two dice at the same time, an n
number of times in a row
CodePudding user response:
I know I only made this post about 20 minutes ago but I actually already managed to figure it out. The solution is:
trials <-lapply(apply(expand.grid(rep(list(rolls), times = n)), 1, identity), unlist)
CodePudding user response:
Assuming you want permutations (both 1,2 and 2, 1) and not combinations (just 1,2), this is simpler:
n <- 2
perms <- expand.grid(as.data.frame(matrix(1:6, 6, n)))
dim(perms)
# [1] 36 2
n <- 4
perms <- expand.grid(as.data.frame(matrix(1:6, 6, n)))
dim(perms)
#[1] 1296 4
n <- 6
perms <- expand.grid(as.data.frame(matrix(1:6, 6, n)))
dim(perms)
# [1] 46656 6