R studio公平骰子概率

y3bcpkx1  于 2023-11-14  发布在  其他
关注(0)|答案(2)|浏览(93)

假设有一个实验,掷100个骰子,然后把每个骰子的结果加起来,计算平均结果。
我如何在R studio中做到这一点,以及如何在Rstudio中绘制概率分布图

fd3cxomn

fd3cxomn1#

我只是举个例子
X表示正态分布的pdf的自变量,把x看作Z分数也很有用,让我用dnorm来表示正态分布的pdf,

z_scores <- seq(-3, 3, by = .1) # First I'll make a vector of Z-scores

# Let's make a vector of the values the function takes given those Z-scores.
# Remember for dnorm the default value for mean is 0 and for sd is 1.

dvalues <- dnorm(z_scores) 

# Now we'll plot these values
plot(dvalues, # Plot where y = values and x = index of the value in the vector
     xaxt = "n", # Don't label the x-axis
     type = "l", # Make it a line plot
     main = "pdf of the Standard Normal",
     xlab= "Z-score") 

# These commands label the x-axis
axis(1, at=which(dvalues == dnorm(0)), labels=c(0))
axis(1, at=which(dvalues == dnorm(1)), labels=c(-1, 1))
axis(1, at=which(dvalues == dnorm(2)), labels=c(-2, 2))

字符串


的数据

0sgqnhkj

0sgqnhkj2#

这应该能让你给予一个解决问题的开始。

# define the number of rolls
n <- 100

# sample with replacement draws n times from the numbers 1:6
dice <- sample(1:6, n, replace=TRUE)

# calculate the sum of simluated dice rolls
dice_sum <- sum(dice)

# calculate the average outcome 
result <- dice_sum/n

# draw a histogram with a density plot
hist(dice, breaks=seq(0,6, 0.5), probability = TRUE, col = rainbow(12))
lines(density(dice))

字符串

相关问题