Self-Study

抽牌後的預期號碼,直到我得到 A、2、3 等等

  • August 12, 2013

我在解決以下問題時遇到了一些麻煩。

你從一副標準的 52 張牌組中抽牌,直到你得到一張 A。你從剩餘的牌中抽牌直到你得到 2。你繼續抽到 3。在整個牌組用完後你的預期數字是多少?

所以這個問題本質上等於計算出你將在當甲板用完時,即:

我理解了

但無法進一步…

按照@gung 的想法,我相信期望值是5.84?根據我對評論的解釋,我假設“A”是一個幾乎不可能的值(除非套牌中的最後四張牌都是 A)。這是 100,000 次迭代蒙特卡羅模擬的結果

results
   2     3     4     5     6     7     8     9     J     K     Q     T 
1406  7740 16309 21241 19998 15127  9393  4906   976   190   380  2334 

這是R代碼,以防你想玩它..

# monte carlo card-drawing functions from here
# http://streaming.stat.iastate.edu/workshops/r-intro/lectures/5-Rprogramming.pdf

# create a straightforward deck of cards
create_deck <-
   function( ){
       suit <- c( "H" , "C" , "D" , "S" )
       rank <- c( "A" , 2:9 , "T" , "J" , "Q" , "K" )
       deck <- NULL
       for ( r in rank ) deck <- c( deck , paste( r , suit ) )
       deck
   }

# construct a function to shuffle everything
shuffle <- function( deck ){ sample( deck , length( deck ) ) }

# draw one card at a time
draw_cards <-
   function( deck , start , n = 1 ){
       cards <- NULL

       for ( i in start:( start + n - 1 ) ){
           if ( i <= length( deck ) ){
               cards <- c( cards , deck[ i ] )
           }
       }

       return( cards )
   }

# create an empty vector for your results
results <- NULL

# run your simulation this many times..
for ( i in seq( 100000 ) ){
   # create a new deck
   sdeck <- shuffle( create_deck() )

   d <- sdeck[ grep('A|2' , sdeck ) ]
   e <- identical( grep( "2" , d ) , 1:4 )

   # loop through ranks in this order
   rank <- c( "A" , 2:9 , "T" , "J" , "Q" , "K" )

   # start at this position
   card.position <- 0

   # start with a blank current.draw
   current.draw <- ""

   # start with a blank current rank
   this.rank <- NULL

   # start with the first rank
   rank.position <- 1

   # keep drawing until you find the rank you wanted
   while( card.position < 52 ){

       # increase the position by one every time
       card.position <- card.position + 1

       # store the current draw for testing next time
       current.draw <- draw_cards( sdeck , card.position )

       # if you draw the current rank, move to the next.
       if ( grepl( rank[ rank.position ] , current.draw ) ) rank.position <- rank.position + 1

       # if you have gone through every rank and are still not out of cards,
       # should it still be a king?  this assumes yes.
       if ( rank.position == length( rank ) ) break        

   }

   # store the rank for this iteration.
   this.rank <- rank[ rank.position ]

   # at the end of the iteration, store the result
   results <- c( results , this.rank )

}

# print the final results
table( results )

# make A, T, J, Q, K numerics
results[ results == 'A' ] <- 1
results[ results == 'T' ] <- 10
results[ results == 'J' ] <- 11
results[ results == 'Q' ] <- 12
results[ results == 'K' ] <- 13
results <- as.numeric( results )

# and here's your expected value after 100,000 simulations.
mean( results )

引用自:https://stats.stackexchange.com/questions/67179

comments powered by Disqus