SlideShare a Scribd company logo
1 of 45
Rで麻雀 2010.07.24 @mangantempy
アジェンダ 自己紹介 このLTのねらい 麻雀のルール 下準備 面子の作りやすさ あがれる確率
自己紹介 ID : @mangantempy 職業: Webエンジニア 学生時代: 数理情報科学授業についていけず数学嫌いに。 好きな物: 一風堂、マンガ、麻雀
このLTのねらい Rを使ってみる 麻雀の研究 麻雀の布教
麻雀のルール 互いに持ち点を奪い合うゲーム 25,000 0 Aさん Bさん Cさん Dさん
麻雀のルール 互いに持ち点を奪い合うゲーム 25,000 0 Aさん Bさん Cさん Dさん
麻雀のルール 互いに持ち点を奪い合うゲーム 25,000 0 Aさん Bさん Cさん Dさん
麻雀のルール 互いに持ち点を奪い合うゲーム 25,000 0 Aさん Bさん Cさん Dさん
麻雀のルール 136枚の「雀牌」を使う 萬子(マンズ) 筒子(ピンズ) 索子(ソウズ) 字牌(ジハイ) 34種類 * 各4枚 = 計136枚
34種類 * 各4枚 = 計136枚
Rでやると・・・ > ( 9 + 9 + 9 + 7 ) * 4 [1] 136 >
麻雀のルール 和了(あがり)の形 14枚 3枚 3枚 3枚 3枚 2枚 順子(シュンツ) 刻子(コウツ) 雀頭(アタマ) 種類が同じで、 数字が3つ連続するような 3枚の牌の組み合わせ 同じ牌を3枚 同じ牌を2枚
Rでやると・・・ > ( 3 * 4 ) + 2 [1] 14 >
麻雀のルール 一翻役 平和 断ヤオ九 一盃口 立直 門前清自摸 翻牌 海底摸月 河底撈魚 嶺上開花 搶槓 一発 ドラ 二翻役 三色同順 一気通貫 対々和 七対子 全帯ヤオ 混老頭 三暗刻 三色同刻 三槓子 小三元 ダブルリーチ 三翻役 純全帯ヤオ 混一色 二盃口 役満 九連宝燈 緑一色 四暗刻 四槓子 清老頭 国士無双 大三元 小四喜 大四喜 字一色 天和 地和 人和 キレイな手ほど 点数が高い。 六翻役 清一色
下準備 ここで一度麻雀からはなれて、 確率とRの一般的な話を。
「階乗」をRでやると・・・ 𝑛!   > n <- 5 > prod(1:n)# 0! = 1 にならない [1] 120 > gamma(n+1) [1] 120 > factorial(1:5)	# R 1.9 より登場 [1] 120
「組み合わせ」をRでやると・・・ 𝑛𝐶𝑟=𝑛!𝑟!𝑛−𝑟!   > factorial(n) / (factorial(r) * factorial(n-r)) [1] 10 > choose(n, r) [1] 10
「順列」をRでやると・・・ 𝑛𝑃𝑟=𝑛!𝑛−𝑟!   > r <- 2 > factorial(n) / factorial(n-r) [1] 20 > perm <- function(n, r) +   factorial(n) / factorial(n-r) > perm(n, r) [1] 20
「総和」をRでやると・・・ 𝑥=1𝑛𝑓𝑥=𝑓(1)+𝑓(2)+…+𝑓(𝑛)   > myfunc <- function(x) (x+1)^2 > sapply(1:5, myfunc) [1] 4  9 16 25 36 > sum(sapply(1:5, myfunc)) [1] 90
【問題】 袋の中に全部で m 個のボールが入っており、 そのうち、アタリが t 個、ハズレが m-t 個とする。 この中から無作為に 1 個のボールを取り出し、 袋には戻さないという操作を k 回行う時、 k 回目で初めて当たりを引く確率は?
【答え】 k-1 回連続でハズレを引く確率は、 k 回目でアタリを引く確率は、 求める確率は、 𝑃1=(𝑚−𝑡)𝑃(𝑘−1) 𝑚𝑃(𝑘−1)   𝑃2=𝑡 (𝑚−(𝑘−1))   𝑃𝑎𝑡𝑎𝑟𝑖(𝑚,𝑡,𝑘)=𝑃1∗𝑃2=𝑚−𝑡𝑃𝑘−1∗𝑡 𝑚𝑃𝑘  
Rでやると・・・ 𝑃𝑎𝑡𝑎𝑟𝑖(𝑚,𝑡,𝑘)=𝑚−𝑡𝑃𝑘−1∗𝑡 𝑚𝑃𝑘   > Patari <- function(m, t, k) { +   if ((m-t) < (k-1) || m < k) 0  # エラー処理 +   else perm(m-t, k-1) * t / perm(m, k) # main + } 5個(アタリは 2 個)から、3 回目ではじめてアタリを引く確率 > Patari(5, 2, 3) [1] 0.2
これで 道具は揃った。 話を麻雀に戻します。
面子の作りやすさ 刻子が4つある場合… ⇒役は「四暗刻」。	32,000点~ 順子が4つある場合… ⇒役は「平和」。		1,000点~ なんでそんなに違うの!?
刻子と順子   を使った面子は・・・ 順子(シュンツ) 刻子(コウツ)
刻子と順子   を使った面子を作る。有効牌は? 順子(シュンツ) 刻子(コウツ) 有効牌:1種3枚
刻子と順子   を使った面子を作る。有効牌は? 順子(シュンツ) 刻子(コウツ) 有効牌:1種3枚 有効牌:4種8枚
こんなシチュエーションを想定 今  を1枚と、無関係な牌2枚を持っていると仮定して、     (1) 残りの牌の中からランダムな牌を1枚引き、     (2) 不要な牌を1枚捨てる という操作を10回行うとき、   を含む刻子or順子が完成する確率は?
刻子を作るには
順子を作るには
こんなシチュエーションを想定 今  を1枚と、無関係な牌2枚を持っていると仮定して、     (1) 残りの牌の中からランダムな牌を1枚引き、     (2) 不要な牌を1枚捨てる という操作を10回行うとき、   を含む刻子or順子が完成する確率は?
つまり・・・ 全部で牌は136枚。 自分が最初に3枚持っているので、 残りは 136-3=133枚。 有効牌は、1種のみで残り3枚。 10回以内に、 3枚の有効牌のうち少なくとも2枚を引く。
こう考える。 3枚の有効牌を含む133枚の牌の中から  i 回目ではじめて有効牌を引き、かつ 2枚の有効牌を含む133-i 枚の牌の中から  j 回目ではじめて有効牌を引く。 求める確率は、1≦i<10, 1≦j≦10-iとなる すべての i, j についての確率の総和なので、 𝑃=𝑖=19𝑗=110−𝑖𝑃𝑎𝑡𝑎𝑟𝑖(133,3,𝑖)𝑃𝑎𝑡𝑎𝑟𝑖(133−𝑖,2,𝑗)  
Rでやると・・・ 𝑃=𝑖=19𝑃𝑎𝑡𝑎𝑟𝑖(133,3,𝑖)𝑗=110−𝑖𝑃𝑎𝑡𝑎𝑟𝑖(133−𝑖,2,𝑗)   > Pkotu <- function(zen, yuko, kaisu) { +   sum(sapply(1:(kaisu-1), function(i) { +     sum(sapply(1:(kaisu-i), function(j) { +       Patari(zen, yuko, i) * Patari(zen-i, yuko-1, j) +     })) +   })) + } > Pkotu(133,3,10) [1] 0.01475323
試行回数別に確率を見てみる > ( result <- sapply(2:133,  +   function(x) Pkotu(133,3,x)) )  [1] 0.0003417635 0.0010200727 0.0020297099 0.0033654574 0.0050220972   [6] 0.0069944118 0.0092771832 0.0118651939 0.0147532259 0.0179360615  [11] 0.0214084830 0.0251652727 0.0292012126 0.0335110851 0.0380896725  : [121] 0.9820639385 0.9852467741 0.9881348061 0.9907228168 0.9930055882 [126] 0.9949779028 0.9966345426 0.9979702901 0.9989799273 0.9996582365 [131] 1.0000000000 1.0000000000
試行回数別に確率を見てみる > barplot(x, names=2:133)	#棒グラフ
順子を作るには
以下、同様に。 刻子の時と同様に確率を求める。 パターンが多いから、場合分けが必要   ⇒   ⇒   ⇒   ⇒   ⇒   ⇒ グラフ化して、刻子のグラフと見比べる。 順子の方が断然作りやすい事が分かる!
おしまい ありがとうございました。 @mangantempy

More Related Content

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Featured (20)

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 

Rで麻雀