I devoted a day to learn and play with RStudio to generate random values. Just a cool stuff to know later when more serious probabilities will be analysed.

I wrote my self-made functions for rolling and also draw simple charts with results.

### Rolling 6 sided dice

This is how it looks when just run

dice.roll <- function(n){sample(1:n, size = 1)} dice.roll(6) for (i in c(1:10)){ print(dice.roll(6)) }

[1] 4

[1] 1

[1] 5

[1] 4

[1] 1

[1] 2

[1] 6

[1] 2

[1] 3

[1] 1

and this is how it looks like when chart drawn. Remember to vectorize function.

dice.roll <- function(n){sample(1:6, size = 1)} dice.roll <- Vectorize(dice.roll) plot(dice.roll(1:100), type="o", col="blue", xlab="Times rolled", ylab="Value rolled") title(main="Rolling dice", col.main="red", font.main=4)

### Rolling 12 sided dice

> for (i in c(1:10)){ print(dice.roll(12)) }

[1] 8

[1] 1

[1] 12

[1] 3

[1] 2

[1] 10

[1] 9

[1] 10

[1] 4

[1] 1

Chart

dice.roll <- function(n){sample(1:12, size = 1)} dice.roll <- Vectorize(dice.roll) plot(dice.roll(1:100), type="o", pch=23, lty=2, col="green", xlab="Times rolled", ylab="Value rolled") title(main="Rolling 12-sided dice", col.main="brown", font.main=2)

### 2-sided – like tossing a coin

> for (i in c(1:10)){ print(dice.roll(2)) }

[1] 1

[1] 2

[1] 1

[1] 1

[1] 1

[1] 2

[1] 1

[1] 2

[1] 1

[1] 1

Chart

dice.roll <- function(n){sample(1:2, size = 1)} dice.roll <- Vectorize(dice.roll) plot(dice.roll(1:100), type="l", pch=20, lty=1, col="violet", xlab="Times tossed", ylab="Value tossed") title(main="Tossing a coin", col.main="dark blue", font.main=2)

## Let’s walk through some very simple probability examples.

**Q1: What is the probability to roll equal numbers rolling 2 dices one time?**

To find that I am going to understand the space of good rolls and probability then is *good vs all*.

c <- expand.grid(x=1:6, y=1:6) cat("total possible combinations for two dice are ", nrow(c)) cat("I can have equal numbers rolled in ", length(which(c[1] == c[2])), " ways") pA <- length(which(c[1] == c[2]))/nrow(c) cat("Probability p(A) to roll equal numbers is: ", pA)

total possible combinations for two dice are 36

I can have equal numbers rolled in 6 ways

Probability p(A) to roll equal numbers is:0.1666667

**Q2: What is the probability to roll sum between 7 and 10 when rolling 2 dices?**

cat("combinations to have sum between 7 and 10 are ",length(which(rowSums(c) >= 7 & rowSums(c) <= 10))) pB <- length(which(rowSums(c) >= 7 & rowSums(c) <= 10))/nrow(c) cat("Probability p(B) to roll sum between 7 and 10, is: ", pB)

combinations to have sum between 7 and 10 are 18

Probability p(B) to roll sum between 7 and 10, is:0.5

**Q3: What is the probability to roll sum 2 or 7 or 8 when rolling 2 dices?**

cat("sum is 2 or 7 or 8 combinations are ",length(which(rowSums(c) == 2 | rowSums(c) == 7 | rowSums(c) == 8))) pC <- length(which(rowSums(c) == 2 | rowSums(c) == 7 | rowSums(c) == 8)) / nrow(c) cat("Probability p(C) to get sum 2 or 7 or 8, is: ", pC)

sum is 2 or 7 or 8 combinations are 12

Probability p(C) to get sum 2 or 7 or 8, is:0.3333333

### Draw probabilities of sequential Heads for coin or Six for dice

coins <- function(x) { 1/2^(x-1) } dice <- function(x) { 1/6^(x-1) } x<-2:15 plot(x,coins(x),type="l",ylim=c(0,1),col="red", lwd=3,lty=3,main="Probability of sequential Head for coin or six for dice", ylab="probability", xlab="rolls count", log="x", axes=F) axis(2,at=seq(0,1,0.1),labels=T) axis(1,at=seq(0,max(x),1),labels=T) lines(x,dice(x),type="l",col="blue",lwd=2,lty=2) legend(2, 1, legend=c("Coins", "Dice"), col=c("red", "blue"), lty=3:2, cex=0.9)

### Let’s roll three dice ten times

dice.roll <- function(x,n){sample(1:x, size = n, replace=TRUE)} dice.roll(6,3) for (i in c(1:10)){ print(dice.roll(6,3)) }

[1] 3 3 3

[1] 2 2 5

[1] 6 5 1

[1] 6 4 6

[1] 4 1 5

[1] 2 1 1

[1] 6 3 6

[1] 5 6 1

[1] 5 6 4

[1] 3 6 1

**Q4: When three dice rolled, what is the probability to have at least one “1” at a condition that different numbers are rolled?**

Conditional probability is easy: P(A|B) = P(A ∩ B) / P(B)

- A is “have at least one 1”
- B is “roll different numbers” – not always happens, so a probability to be found

I’ll do now like engineers do – just calculate sample sizes :) without formulas

c <- expand.grid(x=1:6, y=1:6, z=1:6) cat("P(A ∩ B) Combination count when at least one is 1 and different numbers rolled",length(which(c[1] != c[2]&c[2] != c[3]&c[1] != c[3]&(c[1] == 1 | c[2] == 1 | c[3]==1))), " and total count of combinations is ", nrow(c)) pAB <- length(which(c[1] != c[2]&c[2] != c[3]&c[1] != c[3]&(c[1] == 1 | c[2] == 1 | c[3]==1)))/nrow(c) cat("P(B) = combinations count when different numbers rolled ",length(which(c[1] != c[2]&c[2] != c[3]&c[1] != c[3])), " and total count of combinations is", nrow(c)) pB <- length(which(c[1] != c[2]&c[2] != c[3]&c[1] != c[3]))/nrow(c) cat("P(A|B) = P(A ∩ B) / P(B) = ", pAB / pB)

P(A ∩ B) Combination count when at least one is 1 and different numbers rolled 60 and total count of combinations is 216

P(B) = combinations count when different numbers rolled 120 and total count of combinations is 216

P(A|B) = P(A ∩ B) / P(B) =0.5

Next time – some of binomials. I wish I had 48 hours a day.