在R中创建俄罗斯轮盘赌游戏

在R中创建俄罗斯轮盘赌游戏,r,R,我想知道是否有人能帮我在R中创建一个俄罗斯轮盘赌游戏?我在R和编写代码方面完全是新手。我将非常感谢你的帮助 游戏是用左轮手枪进行的,左轮手枪配有一个可旋转的六发弹匣。左轮手枪装有一发子弹。第一个二重唱,A,随机旋转弹匣,将左轮手枪指向他的头部并按下扳机。如果之后他还活着,他将左轮手枪交给另一名二重唱B,B的动作与A相同。球员们以这种方式交替射击, 直到枪响 到目前为止,我(在帮助下)所想到的是: shot<-runif(1,min=0,max=1) killed<-runif(1,m

我想知道是否有人能帮我在R中创建一个俄罗斯轮盘赌游戏?我在R和编写代码方面完全是新手。我将非常感谢你的帮助

游戏是用左轮手枪进行的,左轮手枪配有一个可旋转的六发弹匣。左轮手枪装有一发子弹。第一个二重唱,A,随机旋转弹匣,将左轮手枪指向他的头部并按下扳机。如果之后他还活着,他将左轮手枪交给另一名二重唱B,B的动作与A相同。球员们以这种方式交替射击, 直到枪响

到目前为止,我(在帮助下)所想到的是:

shot<-runif(1,min=0,max=1)
killed<-runif(1,min=0,max=1/6)
roulette<-function()
{

    while  (shot!=killed) 
    { 
        shot <- runif(1,min=0,max=1)
        print("click")
        if (shot<killed)
            break
    } 
    {
        print ("Dead")
    }
}
for (i in 1:5)
{
    roulette()
}

shot您可以使用while语句并计算迭代次数

奇数发射次数=A模具,偶数发射次数=B模具

n <- 1000
outcomes <- cbind(rep("click",n),rep("click",n),numeric(length=n),numeric(length=n))
colnames(outcomes) <- c("finalA","finalB","nClickA","nClickB")

for (i in 1:nrow(outcomes)){

  shot<-runif(1,min=0,max=1) #first shot
  count <- 1

  while (shot > 1/6) { #chance of dying is 1/6, so every 0 < shot < 1/6
                       #will be considered "death"
    shot <- runif(1,min=0,max=1) #if not dead, shoot again
    count <- count + 1 #and count the iterations
    }

  #replace "click" by "dead" for either A or B
  ifelse ((count %% 2) > 0, #if count is odd number = A killed, else: B killed
      (outcomes[i,1] <- "dead"), #replace the "click"s in outcomes matrix
      (outcomes[i,2] <- "dead"))

  #count and insert the number of clicks for each player
  #if count = odd number, both had (count/2)-1 clicks
  nclick <- count- 1 #the number of clicks is always the number of triggerpulls minus 1

  if ((nclick %% 2) == 0) {outcomes[i,3:4] <- nclick/2
  #if nclick is even number, both A and B took (nclick/2) clicks

  } else {
          outcomes[i,3] <- nclick/2+0.5
          outcomes[i,4] <- nclick/2-0.5}
  #if nclick is odd number, A took one click more than B
}

outcomes <- as.data.frame(outcomes)
table(outcomes$finalA)
table(outcomes$finalB)

编辑:如果子弹在相邻的房间里怎么办? 如果子弹在相邻的弹室中,则有六个可能的位置,每个弹室有两个。第一次扳机拉力p(死亡)=2/6。如果没有发射子弹,我们知道两个子弹位置都不正确,下一个触发点p(死亡)=1/4,依此类推

count <- 1
k <- 5
death <- sample(c(T,T,F,F,F,F)[1:k],1)
while (death != TRUE){k <- k-1  
                      death <- sample(c(T,F,F,F)[1:k],1)
                      count <- count +1}

count据我所知,这种模式是“不断地来回交易枪支,直到射击次数达到numshots”。我将加载的腔室建模为模具的滚动,将“6”设置为致命滚动

roulette <- function(numshots=1000){
  killed = 6 # fatal face on a die
  killshots = 0
  won = 0
  i = 0
  while (killshots < numshots){
     shot = sample(1:6, size=1, replace=T) # rolling a die
     i = i + 1  # keep track of even or odd # of tries
     if ( (shot == killed) ){
      killshots = killshots + 1
      if (i%%2 == 1){
        won = won + 1
      }
    }
  }
  return(data.frame(A = won, B = numshots-won))
}

> roulette()
    A  B
1 502 498
轮盘赌轮盘赌()
A B
1 502 498
今天早上早些时候,我在看一个更复杂的俄罗斯R型轮盘赌游戏,并决定为一个简单版本制作一个函数。在你的例子中,你有“洗牌”,因为房间在每回合前都会旋转

RR <- function(PLAYERS, S = 6){
    D <- 0
    i <- 0

    while(D != 1){
        P <- sample(c(1, rep(0, times = S-1)))[1]
        i <- i + 1
        if(P == 1){
            D <- 1
            }
    }

    L <- rep(PLAYERS, length.out = i)[i]
    L

    }
但是超过10000场比赛会发生什么

> n <- 10000
> RRres <- rep(NA, times = n)
> 
> for(i in 1:n){
+ RRres[i] <- RR(PLAYERS)
+ }
> 
> table(RRres)
RRres
Eastwood    Wayne 
    5393     4607 
请注意,此功能适用于任意数量的玩家(例如26名玩家):

>用于(1:n中的i){
+RRres[i]
>表(RRres)
RRres
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
396 362 378 368 373 388 383 398 390 372 379 382 395 393 377 389 381 386 375 379 375 382 379 430 393 397

轮盘赌功能不记录谁赢谁输。您可以让该功能返回“赢”或“输”(取决于尝试偶数次还是奇数次后“死”的情况)然后运行这个函数的次数是1000次。B的次数与B的丢失次数相同,反之亦然。@安娜这样做是否解决了你的问题?请考虑确认任何对你的问题有帮助的答案。非常感谢你和@孜然的回答!我想知道你是否可以解释命令“%% 2”。再多一点?我知道%%是一个算术运算符,但我不太理解R教程中的解释:“用第二个向量给出第一个向量的剩余部分”.提前谢谢!还有一个问题:在我看来,你答案中的游戏只有两次试玩?或者我误解了?但是每个游戏在死亡之前都可能有任意数量的点击。如果每一行都是一场游戏的结果(一场胜利,一场死亡),是否有可能以某种方式获得A和B在死亡前的试验次数?例如,在一个游戏中:4次点击(A),4次点击(B)和一次射击(A)。非常感谢!我已尝试编辑游戏,以便在不旋转枪管的情况下拥有一支带两颗子弹的手枪。我使用了示例功能,但无法使其正常工作。在while循环中,即使使用示例,也会显示相同的数字。如何才能显示不同的数字?非常感谢!
> PLAYERS <- c("Eastwood", "Wayne")
> RR(PLAYERS)
[1] "Wayne"
> n <- 10000
> RRres <- rep(NA, times = n)
> 
> for(i in 1:n){
+ RRres[i] <- RR(PLAYERS)
+ }
> 
> table(RRres)
RRres
Eastwood    Wayne 
    5393     4607 
> for(i in 1:n){
+ RRres[i] <- RR(sample(PLAYERS))
+ }
> 
> table(RRres)
RRres
Eastwood    Wayne 
    5017     4983
> for(i in 1:n){
+ RRres[i] <- RR(sample(LETTERS[1:26]))
+ }
> 
> table(RRres)
RRres
  A   B   C   D   E   F   G   H   I   J   K   L   M   N   O   P   Q   R   S   T   U   V   W   X   Y   Z 
396 362 378 368 373 388 383 398 390 372 379 382 395 393 377 389 381 386 375 379 375 382 379 430 393 397