我玩一个在线扑克游戏,我担心在经过许多观察太多非随机分布之后,我看到玩家的初始牌并不是随机的。我试图拿出200-1000手牌并将其与模拟进行比较以比较分布并查看它是否确实是随机的。如果是,我希望统一分布。无论如何,我很难搞清楚如何用每张牌存放两张牌的1000张交易。下面是我的代码,但每个输出打印出相同的" 2C和KC",1000次。替换需要等于假,因为不能处理相同的卡。我非常感谢您解决此模拟的任何帮助,以及任何人可能对此实验所做的任何建议。提前谢谢!
cardDeck <- c("AH", "1H", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "10H", "JH", "QH", "KH",
"AS", "1S", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "JS", "QS", "KS",
"AC", "1C", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "JC", "QC", "KC",
"AD", "1D", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "JD", "QD", "KD")
deal <- function(cardDeck) {
cardOne <- sample(cardDeck, 1, replace = FALSE)
cardTwo <- sample(cardDeck, 1, replace = FALSE)
handDealt <- paste(cardOne, cardTwo, sep = " and ")
}
cardsDealt <- rep(deal(cardDeck), 1000)
答案 0 :(得分:5)
以下是一些改进:
紧凑地生成甲板:
cardDeck <- c(outer(c("A",1:10,"J","Q","K"),
c("H","S","C","D"),
## could use
## "\u2661","\u2662","\u2667", "\u2664"
paste0))
正如评论中所指出的,你需要在一次sample()
电话中选择一手牌,以便在没有替换的情况下获得正确的抽样(如果你想从你需要的单笔交易中抽取多手牌)修改此项以在单个sample()
语句中对所有牌中的所有牌进行采样
deal <- function(n=2,collapse=" and ") {
paste(sample(cardDeck, n, replace=FALSE),
collapse=collapse)
}
根据评论和@ ssdecontrol的答案建议使用replicate()
:
set.seed(101)
replicate(5,deal())
## [1] "6S and 2H" "JC and 8C" "KH and 2S" "4C and 4S" "6C and 2C"
答案 1 :(得分:3)
您希望使用replicate
来评估deal(cardDeck)
1000次,而不是rep
,它会对其进行一次评估,然后将结果广播1000次。这是因为replicate
的参数是expression
,而rep
使用R的典型值传递行为,因为参数在之前被评估/ em>调用该函数。