Skip to content

As part of a course exercise, you were asked to write a short R script that simulates 10 rolls of a pair of dice and writes the results to a text file. This first version gave us a baseline to build on and improve throughout the course. After exploring how to write cleaner, more efficient, and more idiomatic R code, you created a second version of your script that reflects what you learned. These two versions highlight your progress and the improvements in your coding style. Since everyone approached the task a little differently, we've put together a selection of scripts to show the variety of solutions. These examples are here to help you reflect on your own work and see how others tackled the same challenge.

Ednah Version #1
#Set number of rolls
num_rolls<-10

#simulate the dice rolls
set.seed(200)
die1 <- sample(1:6, num_rolls, replace= TRUE)
die2 <- sample(1:6, num_rolls, replace = TRUE)

#Combine results into a data frame
results <- data.frame(Roll = 1:num_rolls, Die1 = die1, Die2 = die2)

#Write results to a text file
write.table(results, file ="GDA_dice_rolls.txt", row.names = FALSE, quote = FALSE, sep ="t")
Hongyuan Version #1
---
title: "GDA25:RollingDice"
author: "Hongyuan Zhang"
date: "2025-06-02"
output: html_document
---

```{r setup, include=FALSE}

library('tidyverse')

## Task

*"Write a script that creates a text file with the results of 10 rolls of a pair of dice."\
*I assume the pair of dice is in D6

```{r Rolling Dice}

set.seed(2025)  # for reproducibility

n_rolls <- 10  # 10 rolls

results <- tibble(
  roll_id = seq_len(n_rolls),
  dice_1 = sample(1:6, n_rolls, replace = TRUE),
  dice_2 = sample(1:6, n_rolls, replace = TRUE),
) %>% 
    write_tsv(., file = "dice_rolls_10.txt") %>%  # save it as a text file
    print(.)  # display the result in the markdown :)
Jonathan

Version #1

#####
#Title: Roll dice
#Author: Jonathan Ohnmacht
#Date: 9.6.25
####

#library

#number of dice
n_dice<-2

#number of rolls
n_rolls<-10

#number of pips
n_pips<-6

#output matrix
Mdice<-matrix(nrow = n_rolls, ncol = n_dice)

#roll the dice
for (i in 1:n_rolls){
  Mdice[i,]<- sample(c(1:n_pips),n_dice,replace = TRUE)
}

#export text based table
write.table(Mdice,file = "Dice_roll.csv",col.names = FALSE,row.names = FALSE) 

Version #2:

#####
#Title: Roll dice
#Author: Jonathan Ohnmacht
#Date: 9.6.25
####

## remove all variables if needed
#rm(list = ls())

#define working directory
setwd("P:/Jonathan_Ohnmacht/Courses/GDA25/Demo")

#set seed
set.seed(123)

#number of dice
n_dice<-2

#number of rolls
n_rolls<-10

#number of pips
n_pips<-6

#source the dice function
source("RollDiceF.R")

#apply the dice function
Mdice<-rollDiceF(n_pips,n_rolls,n_dice)

#export text based table
write.table(Mdice,file = "Dice_roll.csv",col.names = FALSE,row.names = FALSE)
Livia

Version #1:

Below you see the first version of the assignment, which compiles the results of 10 dice rolls with two dice. It saves the results locally in a text file.

# Roll two dice 10 times
die1 <- sample(1:6, 10, replace = TRUE)
die2 <- sample(1:6, 10, replace = TRUE)

# Create readable results
results <- paste("Roll", 1:10, ": Die 1 =", die1, ", Die 2 =", die2)

# Write to file
writeLines(results, "dice_rolls2.txt")

Version #2:

The second version of the assignment shows a table below with the result of 10 simulated rolls of two six-sided dice, is reproducible due to the set seed, and the results are shown in this markdown-file.

---
title: "Dice Rolling - GDC Assignment"
author: "Livia"
date: "2025-06-19"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)


```{r}
set.seed(46)

# Roll dice
die_1 <- sample(1:6, 10, replace = TRUE)
die_2 <- sample(1:6, 10, replace = TRUE)

# Create data frame
results_df <- data.frame(
  Roll = 1:10,
  Die1 = die_1,
  Die2 = die_2
)  

# Show table
knitr::kable(results_df, caption = "Table 1: Results of 10 Dice Rolls")    
Lucie

Version #1:

issues = 1:6
data = sample(issues, 10, TRUE)
write.csv(data, "./data.txt")

Version #2

### set working directory
setwd("~/GDA")

### generate folder
#dir.create("Rolling_dice") #only run once
setwd("Rolling_dice")

## remove all variables
rm(list = ls())

## set seed
set.seed(1000)

# Parameters
number_of_rolls <- 10
number_of_dices <- 4
issues <- 1:6

# Function to roll any number of dice
roll_multiple_dice <- function(n_rolls, n_dices) {
  rolls <- replicate(n_dices, sample(issues, n_rolls, TRUE))
  colnames(rolls) <- paste0("Die", 1:n_dices)
  data <- data.frame(Roll = 1:n_rolls, rolls)
  return(data)
}

# Call the function
rolls_of_dices <- roll_multiple_dice(number_of_rolls, number_of_dices)

# Save the results
write.csv(
  rolls_of_dices,
  paste0(number_of_rolls, "rolls_of_", number_of_dices, "dices.csv"),
  row.names = FALSE
)
Natalia Version #1
install.packages("EnvStats")
library(EnvStats)

#Considering that throwing a pair a dice follows a triangular probability function with
#minimum of 2, maximum of 12 and mode of 7 is the most frequently, the following
#function selects a sample of 10 random numbers from the distribution, representing the scenario
# of 10 rolls of a pair of dice.

sample <- round(rtri(10, min = 2, max = 12, mode = 7))

#Creating a file to show the results
line <- c("The results of 10 rolls of a pair of dice are:", sample)
writeLines(line, "10_dice_rolls_NM.txt")
Sotiria

Version #1

# Rolling Dice Assignment
# Author: Sotiria

setwd("/Users/smilia/Documents/Genetic_diversity/")
install.packages('dndR')
install.packages("stringi")
libraries = c("dndR","stringi")

#sum of 2- six sided dice, results of 10 rounds
num_rolls = 10
results = integer(num_rolls)
for (i in 1:num_rolls) {
  rollings_sum = roll(dice = "1d6") + roll(dice = "1d6")
  results[i] = sprintf("Roll %d: %d", i, rollings_sum)
}
results

write.table(results, file = "dice_rolls.txt", row.names = FALSE, col.names = FALSE)

Version #2

# Rolling Dice Assignment - Improved Version
# Author: Sotiria
# Title: Rolling of two six-sided dice for ten rounds

setwd("/Users/smilia/Documents/Courses/Genetic_Diversity_Analysis/")

# install and load required packages
required_packages <- c("dndR", "stringi")

# check if packages are installed, install if not
for (pkg in required_packages) {
  if (!require(pkg, character.only = TRUE)) {
    install.packages(pkg)
    library(pkg, character.only = TRUE)
  }
}

## remove all variables
rm(list = ls())

#sum of 2- six sided dice, results of 10 rounds
num_rolls <- 10
set.seed(250617)  # For reproducible results

results <- integer(num_rolls)

for (i in 1:num_rolls) {
  rollings_sum = roll(dice = "1d6") + roll(dice = "1d6")
  # create formatted results for display-output
  results[i] = sprintf("Roll %d: %d", i, rollings_sum)
}
results

# Save results to file
write.table(results, file = "dice_rolls_revised.txt", row.names = FALSE, col.names = FALSE)
Violeta

Version #1:

# Working directory
setwd("D:/Users/hp/Desktop/Master/Genetic Diversity Analysis")

# Number of rolls
n_rolls <- 10

# Create an empty matrix to store results (Roll, Die1, Die2, Sum)
results <- matrix(nrow = n_rolls, ncol = 4)

# Simulate the dice rolls
for (i in 1:n_rolls) {
  die1 <- sample(1:6, 1)
  die2 <- sample(1:6, 1)
  total <- die1 + die2
  results[i, ] <- c(i, die1, die2, total)
}

# Convert matrix to data frame and add column names
results_df <- as.data.frame(results)
colnames(results_df) <- c("Roll", "Die1", "Die2", "Sum")

# Write to text file
write.table(results_df, "dice_rolls.txt", sep = "\t", row.names = FALSE)

Version #2:

---
title: "Rolling Dices - imporved version"
author: "Violeta Marie"
date: "`r Sys.Date()`"
output:
  pdf_document: default
  html_document: default
---

```{r , include=FALSE}
# remove all variables
rm(list = ls())

## Make randomly generated numbers predictable
```{r}
set.seed(123)

## Number of rolls
Define how many times you want to roll the dice. 

```{r}
nrolls <- 10

## Number of dice
Define how many times you want to roll the dice. 

```{r}
ndice <- 2

## Roll function
Decfine a function to roll the dice "nroll"-times. This function rolls "ndice" number of dice. 

```{r, roll function}

roll_fun <- function(nrolls, ndice){

# Create an empty matrix to store results (Roll, D1, D2,..., Sum)
results <- matrix(nrow = nrolls, ncol = ndice + 2)
colnames(results) <- c("Roll", paste0("D", 1:ndice), "Tot")

# Roll the dice nroll-times
for (i in 1:nrolls) {
  dice_vals <- sample(1:6, ndice, replace = TRUE)
  total <- sum(dice_vals)
  results[i, ] <- c(i, dice_vals, total)
}
# Save the results
return(results)
}

## Run the function
Use the function and save results. 

```{r}
dice.results <- roll_fun(nrolls, ndice)

## Print the results
Display the results. 

```{r}
print(dice.results)

## Export as a table 
```{r}
write.table(dice.results, "dice_rolls_2.txt", sep = "\t", row.names = FALSE)

## Print session information

```{r}
writeLines(capture.output(sessionInfo()))