Hello All, A colleague of mine started working with R and out of curiosity I did some research on the language. Very nice. In my opinion this is one of the best languages I've found for getting tasks I'm interested in done. I wrote this simple die roller and was curious to know if it is R enough. ############################################################################## # # Input: # die_size - 4, 6, 8, 10, 20 # number_of_dice - How many dice to roll # number_of_rolls - How many times to roll the dice # # Output: # rolls - The array holding the values of the dice # ############################################################################## # function(die_size, number_of_dice, number_of_rolls) { rolls <- array(0, dim=c(number_of_rolls, number_of_dice)) for (i in 1:number_of_rolls) { rolls[i,] <- sample(die_size, number_of_dice, replace=TRUE) } return(rolls) } Any thoughts on this function? Is there a better way of doing it in R? Can you tell I used to play AD&D? Thanks, Joe
Try this: matrix(sample(dsize, nrolls * ndice, replace = FALSE), nrolls, ndice) On Mon, Feb 2, 2009 at 12:39 PM, Joe Hughes <joe.hughes at earthlink.net> wrote:> Hello All, > > A colleague of mine started working with R and out of curiosity I did > some research on the language. Very nice. In my opinion this is one of the > best languages I've found for getting tasks I'm interested in done. I wrote > this simple die roller and was curious to know if it is R enough. > > ############################################################################## > # > # Input: > # die_size - 4, 6, 8, 10, 20 > # number_of_dice - How many dice to roll > # number_of_rolls - How many times to roll the dice > # > # Output: > # rolls - The array holding the values of the dice > # > ############################################################################## > # > function(die_size, number_of_dice, number_of_rolls) > { > rolls <- array(0, dim=c(number_of_rolls, number_of_dice)) > > for (i in 1:number_of_rolls) > { > rolls[i,] <- sample(die_size, number_of_dice, replace=TRUE) > } > > return(rolls) > } > > Any thoughts on this function? Is there a better way of doing it in R? Can > you tell I used to play AD&D? > > Thanks, > Joe > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
If you are interested in rolling dice with R (as opposed to using this as a simple test case to start writing your own programs), then you may want to look at the "dice" function from the TeachingDemos package. The core line in this function is basically the same as Gabor's suggestion, but with the added option of weighting the dice (you don't like how I graded your exam? I am happy to let these computerized dice regrade it for you :-). There is also an option to plot the results (for 6-sided dice). There is also the plot.rgl.die and roll.rgl.die functions in the same package that give an animated rolling of the die (I really need to rewrite it to get a better animation, the coin flip works ok as is). Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Joe Hughes > Sent: Monday, February 02, 2009 10:40 AM > To: R-help at r-project.org > Subject: [R] New to R > > Hello All, > > A colleague of mine started working with R and out of curiosity I > did > some research on the language. Very nice. In my opinion this is one > of the best languages I've found for getting tasks I'm interested in > done. I wrote this simple die roller and was curious to know if it is > R enough. > > ####################################################################### > ####### > # > # Input: > # die_size - 4, 6, 8, 10, 20 > # number_of_dice - How many dice to roll > # number_of_rolls - How many times to roll the dice > # > # Output: > # rolls - The array holding the values of the dice > # > ####################################################################### > ####### > # > function(die_size, number_of_dice, number_of_rolls) > { > rolls <- array(0, dim=c(number_of_rolls, number_of_dice)) > > for (i in 1:number_of_rolls) > { > rolls[i,] <- sample(die_size, number_of_dice, replace=TRUE) > } > > return(rolls) > } > > Any thoughts on this function? Is there a better way of doing it in > R? Can you tell I used to play AD&D? > > Thanks, > Joe > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.
Joe Hughes wrote:> > # > function(die_size, number_of_dice, number_of_rolls) > {you may want to replace> rolls <- array(0, dim=c(number_of_rolls, number_of_dice)) > > for (i in 1:number_of_rolls) > { > rolls[i,] <- sample(die_size, number_of_dice, replace=TRUE) > }with, e.g. rolls = t(replicate(number_of_rolls, sample(die_size, number_of_dice, replace=TRUE))) to have it more r-ish> > return(rolls) > } > > Any thoughts on this function?as soon as you start wrapping something like subset, lm, or the like inside your functions, all hell breaks loose. welcome to the r inferno, that is. vQ
All, Thanks for taking the time to reply. I understand a bit more about R and the R way then I did before. The final function looks like this: ############################################################################## # # Input: # die_size - 4, 6, 8, 10, 20 # number_of_dice - How many dice to roll # number_of_rolls - How many times to roll the dice # # Output: # The array holding the results of the rolls # ############################################################################## # function(die_size, number_of_dice, number_of_rolls=1) { return(t(replicate(number_of_rolls, sample(die_size, number_of_dice, replace=TRUE)))) } Before I take a look at the teaching demos, I have one question left. Here is a sequence of commands and the output > sample(6, 4, replace=TRUE) [1] 3 4 5 4 > replicate(7, sample(6, 4, replace=TRUE)) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 3 3 6 4 5 6 6 [2,] 4 4 6 5 5 1 6 [3,] 5 1 4 5 6 5 6 [4,] 4 6 3 1 1 2 2 Why does replicate transpose the vector before assigning it to the array? The way I would output it would be this [,1] [,2] [,3] [,4] [1,] 3 4 5 4 [2,] 3 4 1 6 [3,] 6 6 4 3 [4,] 4 5 5 1 [5,] 5 5 6 1 [6,] 6 1 5 2 [7,] 6 6 6 2 Thanks, Joe