# [R] Model To Simulate Dice Roll

Paul Bernal p@u|bern@|07 @end|ng |rom gm@||@com
Thu Apr 21 06:02:39 CEST 2022

```Dear friend Bert,

Thank you so much for your kind reply. The first thing I need to do is to
simulate dice rolls, say 120 times.

I want to populate an m by 12 dataframe with the results of each dice roll.
For example, the result from dice roll #1 would need to go on row 1,
column1, the result from dice roll #2 would have to go in row 1 column 2,
and so on.

The reason why I want to store those results in a dataframe is to be able
to perform some other calculations afterwards.

This is for a project I am doing.

So this is the situation:
You and five friends – a total of six people – plan to meet once per month
to have dinner together, with one of you choosing the restaurant each
month. Rather than scheduling the entire year in advance, you decide to
make it interesting: each month a single six-sided die will be rolled to
determine which of you gets to choose the restaurant that month. How likely
is it that everyone will have a chance to eat at their own favorite
restaurant? That is, what is the probability p that over the next 12
months, each of you will have had at least one opportunity to choose where
to eat?

This is what I am asked to do:
Write a program to estimate the desired probability p via simulation. The
program should input a sequence of positive integer number of trials to
simulate using the language's pseudorandom number generator and calculate
the corresponding fractions of simulated trials that are “successful"
(i.e., all 6 parties get at least one opportunity to choose where to eat.
Alice, Bob, Charley, Fred, Ellen, Don, Don, Don, Don, Alice, Charley, Bob
is a successful trial. Alice, Bob, Charley, Ellen, Don, Don, Don, Don,
Ellen, Alice, Charley, Bob is not a successful trial since Fred does not
get to choose.)
Turn in a set of 10 trials showing each roll of the dice to show
correctness. Label the out-comes. Keep in mind that a single trial requires
rolling the die twelve times. Calculate and print the average probability p

For this reason, I am trying to simulate the n trials, and then populate a
table with the results from the trials. I have to simulate a dice roll dice
for each month and for each row. Rows would be equivalent to years, and
then columns would be equivalent to the month of a particular year.

Once I store the results in a dataframe, everything is much easier.

I installed package dice and performed simulations by doing:
#declaring variables:
#1)dice_rolls which is the number of times the dice will be rolled
#2)num_dice which is the number of dice that will be rolled each time
#3)dice_sides which is the number of sides of the dice
#function dice will take each one of these variables as its parameter to
perform the simulation
dice_rolls = 120
num_dice   = 1
dice_sides = 6

#performing simulation
dice_simul = dice(rolls = dice_rolls, ndice = num_dice, sides = dice_sides,
plot.it = TRUE)

I tried the following, but did not work as expected:

for (i in 1:nrow(dice_simul)){
for(j in 1:ncol(prob_frame)){
for(k in 1:nrow(prob_frame)){
prob_frame[k,j] = dice_simul[i,1]
}
}
}

I apologize for the long explanation.

Best regards,

Paul

El mié, 20 abr 2022 a las 22:47, Bert Gunter (<bgunter.4567 using gmail.com>)
escribió:

> If I understand you correctly, it's simple.
> Matrices in R are vectors with a dimension attribute. By default, they
> are populated column by column. Use 'byrow = TRUE to populate by row
>
> > matrix (1:36, ncol = 12, byrow = TRUE)
>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
> [1,]    1    2    3    4    5    6    7    8    9    10    11    12
> [2,]   13   14   15   16   17   18   19   20   21    22    23    24
> [3,]   25   26   27   28   29   30   31   32   33    34    35    36
>
> I leave it to you to use the 'dimnames' argument of ?matrix  to give
> names to the column and then subsequently convert to a data frame if
> you like.
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along
> and sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
> On Wed, Apr 20, 2022 at 8:38 PM Paul Bernal <paulbernal07 using gmail.com>
> wrote:
> >
> > Dear friends,
> >
> > Hope you are doing well. I need to simulate a 1 dice roll for each one of
> > the twelve months of the year and perform 100 trials, so I thought of
> > constructing a dataframe with twelve columns and 100 rows the following
> way:
> >
> > num_rows = 100
> >
> > prob_frame <- data.frame(matrix(NA, nrow = num_rows, ncol = 12))
> >
> colnames(prob_frame)<-c("January","February","March","April","May","June","July","August","September","October","November","December")
> >
> > Now, using the dice package, I can simulate n number of dice rolls as
> > follows:
> > #performing simulation
> > dice_simul = dice(rolls = dice_rolls, ndice = num_dice, sides =
> dice_sides,
> > plot.it = TRUE)
> >
> > What I would like to do is to populate each column and row with the
> results
> > of dice_simul.
> >
> > Let me show you the structure of dice_simul:
> > > str(dice_simul)
> > Classes ‘dice’ and 'data.frame': 100 obs. of  1 variable:
> >  \$ Red: int  2 2 1 2 5 4 4 6 1 4 ...
> > > dput(dice_simul)
> > structure(list(Red = c(2L, 2L, 1L, 2L, 5L, 4L, 4L, 6L, 1L, 4L,
> > 4L, 2L, 6L, 2L, 2L, 1L, 3L, 6L, 1L, 5L, 5L, 5L, 3L, 4L, 2L, 6L,
> > 4L, 6L, 6L, 2L, 1L, 2L, 2L, 6L, 4L, 2L, 3L, 5L, 6L, 6L, 4L, 5L,
> > 4L, 6L, 6L, 3L, 4L, 1L, 5L, 3L, 3L, 5L, 3L, 4L, 1L, 3L, 3L, 2L,
> > 4L, 1L, 2L, 1L, 6L, 3L, 5L, 5L, 3L, 4L, 4L, 5L, 4L, 1L, 5L, 3L,
> > 4L, 4L, 3L, 6L, 5L, 2L, 4L, 1L, 1L, 6L, 4L, 3L, 6L, 5L, 6L, 2L,
> > 6L, 1L, 6L, 6L, 4L, 3L, 4L, 2L, 1L, 5L)), class = c("dice", "data.frame"
> > ), row.names = c(NA, -100L))
> >
> > For example, the first number of dice_simul should go to row 1 for
> January,
> > the second number of dice_simul should go to row 1 for February, ... the
> > twelveth number of dice_simul should go to row 1 for December, the 13th
> > number should go to row 2 for january, and so on.
> >
> > This is what I tried to do but doesn´t work they way I want to:
> >
> > #1)dice_rolls which is the number of times the dice will be rolled
> > #2)num_dice which is the number of dice that will be rolled each time
> > #3)dice_sides which is the number of sides of the dice
> > #function dice will take each one of these variables as its parameter to
> > perform the simulation
> > dice_rolls = 100
> > num_dice   = 1
> > dice_sides = 6
> >
> > #performing simulation
> > dice_simul = dice(rolls = dice_rolls, ndice = num_dice, sides =
> dice_sides,
> > plot.it = TRUE)
> >
> > num_rows = 100
> >
> > prob_frame <- data.frame(matrix(NA, nrow = num_rows, ncol = 12))
> > colnames(prob_frame) <-
> >
> c("January","February","March","April","May","June","July","August","September","October","November","December")
> >
> >
> > for (j in 1:12){
> >   for (i in 1:num_rows){
> >     prob_frame[i,j]=dice_simul[i,1]
> >   }
> > }
> > I basically want to populate the twelve months for the first row, then
> the
> > twelve months for the second row, and so on, until I get to populate the
> > twelve months for the last row sequentially.
> >
> > How could I accomplish this?
> >
> > Any help and/or guidance will be greatly appreciated.
> >
> > Best regards,
> > Paul
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help