all possible combinations of two dice

Hi. I am new to R language and I am trying to make all possible combinations of two dice [d1 = (1, 3, 5, 7, 9, 11) and d2 = (2, 4, 6, 8, 1, 12)] like this: (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), ... , (6, 4), (6, 5), (6, 6). I did make them using Python list(product(d1, d2)) but can't figure out how I can do that using R. Please help.

library(tidyverse)

n_rolls <- 100
n_sides <- 6

rolls <- tibble(
  d1 = ntile(runif(n_rolls), n_sides),
  d2 = ntile(runif(n_rolls), n_sides),
) 

print(rolls)
#> # A tibble: 100 x 2
#>       d1    d2
#>    <int> <int>
#>  1     5     4
#>  2     5     1
#>  3     3     5
#>  4     6     1
#>  5     3     3
#>  6     5     2
#>  7     1     4
#>  8     2     1
#>  9     4     4
#> 10     3     6
#> # ... with 90 more rows

table(rolls$d1)
#> 
#>  1  2  3  4  5  6 
#> 17 17 17 17 16 16

Created on 2022-01-19 by the reprex package (v2.0.1)

Can you explain a lil bit? As I am new to R so don't know what just happened

Whoops I didn't read your post.

I think you want this instead.

expand.grid(d1 = 1:6, d2 = 1:6)
#>    d1 d2
#> 1   1  1
#> 2   2  1
#> 3   3  1
#> 4   4  1
#> 5   5  1
#> 6   6  1
#> 7   1  2
#> 8   2  2
#> 9   3  2
#> 10  4  2
#> 11  5  2
#> 12  6  2
#> 13  1  3
#> 14  2  3
#> 15  3  3
#> 16  4  3
#> 17  5  3
#> 18  6  3
#> 19  1  4
#> 20  2  4
#> 21  3  4
#> 22  4  4
#> 23  5  4
#> 24  6  4
#> 25  1  5
#> 26  2  5
#> 27  3  5
#> 28  4  5
#> 29  5  5
#> 30  6  5
#> 31  1  6
#> 32  2  6
#> 33  3  6
#> 34  4  6
#> 35  5  6
#> 36  6  6

Created on 2022-01-19 by the reprex package (v2.0.1)

Be aware of the difference between permutation and combination.

The permutation of six objects (the pips on the dice) taken two at a time (the pair of dice) can be shown as follows:

expand.grid(1:6,1:6)
#>    Var1 Var2
#> 1     1    1
#> 2     2    1
#> 3     3    1
#> 4     4    1
#> 5     5    1
#> 6     6    1
#> 7     1    2
#> 8     2    2
#> 9     3    2
#> 10    4    2
#> 11    5    2
#> 12    6    2
#> 13    1    3
#> 14    2    3
#> 15    3    3
#> 16    4    3
#> 17    5    3
#> 18    6    3
#> 19    1    4
#> 20    2    4
#> 21    3    4
#> 22    4    4
#> 23    5    4
#> 24    6    4
#> 25    1    5
#> 26    2    5
#> 27    3    5
#> 28    4    5
#> 29    5    5
#> 30    6    5
#> 31    1    6
#> 32    2    6
#> 33    3    6
#> 34    4    6
#> 35    5    6
#> 36    6    6

This distinguishes outcomes depending on which of the two comes first (think a red die and a white die). To get the possibilities without regard to order, a combination,

combn(1:6,2)
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
#> [1,]    1    1    1    1    1    2    2    2    2     3     3     3     4     4
#> [2,]    2    3    4    5    6    3    4    5    6     4     5     6     5     6
#>      [,15]
#> [1,]     5
#> [2,]     6
2 Likes

Yes. but one dice is having even numbers (2,4,6,8,10,12) and 2nd is having odd numbers (1,3,5,7,9,11) on it

expand.grid(seq(1,11,2),seq(2,12,2))

G.

3 Likes

Hi!

First, I recommend you:

install.packages("gtools")
library(gtools)

#1.- Here you have your_combination

Your_combinations <- combinations(6, 2, v = 1:6)
Your_combinations

#That answer your question!

#2.- EXTRA: Here you have a permutation

Your_permutation <- permutations(6,2, v = 1:6)
Your_permutation

#3.- EXTRA: What if you want a three random tries?

n <- nrow(Your_combinations)
index <- sample(n, 3)
Your_combinations[index,]

Permutations: enumerates the possible permutations.
Combinations: Enumerates the possible combinations of a specified size from the elements of a vector.

Source: combinations function - RDocumentation

Nice day my friend!

The code

Your_combinations <- combinations(6, 2, v = 1:6)
Your_combinations

It don't show all possible 36 combinations. Also, one dice is (1,3,5,7,9,11) and other is (2,4,6,8,10,12)

A combination with 36??

You ask for combination and I gave you combination() function, now If you want to repeat allowed, here you have 21 combinations:

Your_combinations <- combinations(6, 2, v = 1:6, repeats.allowed = T)
Your_combinations

Combinations: Enumerates the possible combinations of a specified size from the elements of a vector.

Your_combinations <- combinations(6, 2, v = 1:6)
Your_combinations
15 :

And when someone gives you a solution, give a solution approvement or at least contribute giving a like as appear in the main page.

Greetings!!
:laughing:

1 Like

Thank you so much for the answer.

R has many options for solving a problem and you need to be able to select the one that is best for your application.

First = c(1, 3, 5, 7, 9, 11)
Second = c(2, 4, 6, 8, 10, 12)
for (i in First){
   for (j in Second){
      print(paste(i, j))
  }
}

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.