simulation of unfair dice

how can i simulate the unfair dice with the use of prob function ?

sample_function <- sample (die =1:4, size=20,replace = TRUE, prob = c(1/2,1/6,1/6,1/6)))

Hi @tanjina,

You are on the right track! You just made a few mistakes in your code and I'll help you correct them. But before I do, I'd like to point out that prob is not a function, but it is an argument of the sample() function. Having said that, here are the mistakes you made:

  • the first argument of the sample() function is always x, so you should have written: x = 1:4 and not die = 1:4.

  • you added an extra closing parenthesis ) that is not needed here.

So the correct code should be:

unfair_die_roll <- sample (x  = 1:4, size = 20, replace = TRUE, prob = c(1/2, 1/6, 1/6, 1/6))

Note that I changed the name of the variable that you created from sample_function to unfair_die_roll even though it does not affect the result.


Thank you so much . i wanted to know that how can i simulate the below result for unfair dice . is it still remain same .

sample_function <- sample (x =1:4, size=20,replace = TRUE, prob = c(1/2,1/6,1/6,1/6))

The simulation that your code performs IS unfair. You have:

  • x = 1:4 and
  • prob = c(1/2, 1/6, 1/6, 1/6)

The reason why it is unfair is because all outcomes do not have the same probability:

  • the outcome 1 has a 50% (or 1/2) chance of landing each time you roll
  • the outcomes 2, 3 and 4 all have a 16.66% (or 1/6) percent chance of landing at each roll

Because all outcomes do not have the same probability, we say that the die is unfair. But actually, you have to remember that a die has 6 sides and not 4. So, the true way of rolling an unfair die would be something like:

sample(x = 1:6, size = 20, replace = TRUE, prob = c(0.2, 0.1, 0.1, 0.3, 0.05, 0.25))

So now, we have 6 sides and all sides have different probabilities of landing.

What you need to remember is that all the probabilities' sum must equal to 1.

1 Like

okay . now i understand. Thank you so much

You are very welcome.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.