[R] Model To Simulate Dice Roll

David Carlson dc@r|@on @end|ng |rom t@mu@edu
Sat Apr 23 06:04:32 CEST 2022


Since the rolls are independent, it is not necessary to separate the rolls
into two stages:

sides <- 6
months <- 12
reps <- 100

set.seed(2022)
results <- matrix(sample.int(sides, months*reps, replace=TRUE), reps,
months, byrow=TRUE)
colnames(results) <- month.name
all6 <- apply(results, 1, function(x) length(intersect(x, 1:6))==6)
sum(all6)/reps
# 0.53 which matches Rui's result
results <- as.data.frame(results)

David L. Carlson



On Thu, Apr 21, 2022 at 4:04 AM Rui Barradas <ruipbarradas using sapo.pt> wrote:

> Hello, There's an error in my code, inline. Às 07:55 de 21/04/2022, Rui
> Barradas escreveu: > Hello, > > For what I understand of the question, the
> followng might answer it. > > The functions below roll dice and simulate R
> replicates
> ZjQcmQRYFpfptBannerStart
> This Message Is From an External Sender
> This message came from outside your organization.
>
> ZjQcmQRYFpfptBannerEnd
>
> Hello,
>
> There's an error in my code, inline.
>
> Às 07:55 de 21/04/2022, Rui Barradas escreveu:
> > Hello,
> >
> > For what I understand of the question, the followng might answer it.
> >
> > The functions below roll dice and simulate R replicates of dice rolls.
> > Then 12 (one per month) 6 sided dice rolls are simulated 100 times.
> >
> > The colMeans/apply computes the empiric probabilities of having all 6
> > sides occur in each row, Jan to Dec and a overall probabilty is the mean
> > of those probabilities.
> >
> > The matrix is coerced to data.frame only at the end.
> >
> >
> >
> > dice <- function(rolls = 1, ndice = 1, sides = 6) {
> >    roll <- function(ndice = 1, sides = 6) {
> >      sample(seq_len(sides), ndice, replace = TRUE)
> >    }
> >    y <- replicate(rolls, roll(ndice = ndice, sides = sides))
> >    if(is.null(dim(y))) y else colSums(y)
> > }
> > dice_simul <- function(rolls = 1, ndice = 1, sides = 6, R) {
> >    if(missing(R)) {
> >      stop("number of simulations 'R' is missing with no default.")
> >    }
> >    replicate(R, dice(rolls = rolls, ndice = ndice, sides = sides))
> > }
> >
> > dice_rolls <- 100
> > #dice_rolls <- 1e6
> > num_dice <- 1
> > dice_sides <- 6
> > months <- 12
> >
> > set.seed(2022)
> > prob_frame <- t(dice_simul(months, num_dice, dice_sides, R = dice_rolls))
> > colnames(prob_frame) <- month.name
> > head(prob_frame)
> >
>
> # --- wrong
> > p <- colMeans(apply(prob_frame, 1, \(x) 1:6 %in% x))
> > mean(p)
> > # [1] 0.9116667
>
> This should be
>
> yes_no <- apply(prob_frame, 1, \(x) all(1:6 %in% x))
> p <- mean(yes_no)
> p
> # [1] 0.53
>
>
> Rui Barradas
>
>
>
> >
> > prob_frame <- as.data.frame(prob_frame)
> >
> >
> > Hope this helps,
> >
> > Rui Barradas
> >
> >
> > Às 05:02 de 21/04/2022, Paul Bernal escreveu:
> >> 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 the set. Please refer to your friends by name.
> >>
> >> 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
> >>> instead. For example:
> >>>
> >>>> 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://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r-help__;!!KwNVnqRv!GmY3d1J5JTqq58eyId3zjOPuWKBidPg_ojtvbhygQyudquxNCQkrcOT1xJfOQTFSlSxEJgiAwDPunCAbZaomfvI$
> >>>> PLEASE do read the posting guide
> >>> https://urldefense.com/v3/__http://www.R-project.org/posting-guide.html__;!!KwNVnqRv!GmY3d1J5JTqq58eyId3zjOPuWKBidPg_ojtvbhygQyudquxNCQkrcOT1xJfOQTFSlSxEJgiAwDPunCAb7rrtTbc$
> >>>> and provide commented, minimal, self-contained, reproducible code.
> >>>
> >>
> >>     [[alternative HTML version deleted]]
> >>
> >> ______________________________________________
> >> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >> https://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r-help__;!!KwNVnqRv!GmY3d1J5JTqq58eyId3zjOPuWKBidPg_ojtvbhygQyudquxNCQkrcOT1xJfOQTFSlSxEJgiAwDPunCAbZaomfvI$
> >> PLEASE do read the posting guide
> >> https://urldefense.com/v3/__http://www.R-project.org/posting-guide.html__;!!KwNVnqRv!GmY3d1J5JTqq58eyId3zjOPuWKBidPg_ojtvbhygQyudquxNCQkrcOT1xJfOQTFSlSxEJgiAwDPunCAb7rrtTbc$
> >> and provide commented, minimal, self-contained, reproducible code.
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r-help__;!!KwNVnqRv!GmY3d1J5JTqq58eyId3zjOPuWKBidPg_ojtvbhygQyudquxNCQkrcOT1xJfOQTFSlSxEJgiAwDPunCAbZaomfvI$
> > PLEASE do read the posting guide
> > https://urldefense.com/v3/__http://www.R-project.org/posting-guide.html__;!!KwNVnqRv!GmY3d1J5JTqq58eyId3zjOPuWKBidPg_ojtvbhygQyudquxNCQkrcOT1xJfOQTFSlSxEJgiAwDPunCAb7rrtTbc$
> > and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, seehttps://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r-help__;!!KwNVnqRv!GmY3d1J5JTqq58eyId3zjOPuWKBidPg_ojtvbhygQyudquxNCQkrcOT1xJfOQTFSlSxEJgiAwDPunCAbZaomfvI$
> PLEASE do read the posting guide https://urldefense.com/v3/__http://www.R-project.org/posting-guide.html__;!!KwNVnqRv!GmY3d1J5JTqq58eyId3zjOPuWKBidPg_ojtvbhygQyudquxNCQkrcOT1xJfOQTFSlSxEJgiAwDPunCAb7rrtTbc$
> and provide commented, minimal, self-contained, reproducible code.
>
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list