【学习资料】袋中共36个球,红白黑格12个,问能一次抽到3个红4个白5个黑的概率是多少?
1、公式计算
1.1 题目1
袋中共 36 36 36个球, 红 \fcolorbox{red}{#FADADE}{\color{red}{红}} 红 白 \fcolorbox{white}{#808080}{\color{white}{白}} 白 黑 \fcolorbox{#808080}{#0D0D0D}{\color{#808080}{黑}} 黑各 12 12 12个,问能一次抽到 3个红 \fcolorbox{red}{#FADADE}{\color{red}{3个红}} 3个红 4个白 \fcolorbox{white}{#808080}{\color{white}{4个白}} 4个白 5个黑 \fcolorbox{#808080}{#0D0D0D}{\color{#808080}{5个黑}} 5个黑的概率是多少?
P m r , m w , m b = C n r m r × C n w m w × C n b m b C n m P_{m_r,m_w,m_b} = \frac{C_{n_r}^{m_r} \times C_{n_w}^{m_w} \times C_{n_b}^{m_b} }{C_{n}^{m}} Pmr,mw,mb=CnmCnrmr×Cnwmw×Cnbmb
其中:
P m r , m w , m b P_{m_r,m_w,m_b} Pmr,mw,mb – 概率
C n m C_{n}^{m} Cnm – n个球中,抽m个球的组合数。
C n r m r C_{n_r}^{m_r} Cnrmr – n r n_r nr个红球中,抽 m r m_r mr个红球的组合数。
C n w m w C_{n_w}^{m_w} Cnwmw – n w n_w nw个白球中,抽 m r m_r mr个白球的组合数。
C n b m b C_{n_b}^{m_b} Cnbmb – n b n_b nb个黑球中,抽 m b m_b mb个黑球的组合数。
根据已知条件代入公式:
已知条件
{
n
r
=
12
n
w
=
12
n
b
=
12
m
r
=
3
m
w
=
4
m
b
=
5
n
=
36
m
=
12
已知条件 \begin{cases} n_r=12 \\ n_w=12 \\ n_b=12 \\ m_r=3 \\ m_w=4 \\ m_b=5 \\ n=36 \\ m=12 \\ \end{cases}
已知条件⎩
⎨
⎧nr=12nw=12nb=12mr=3mw=4mb=5n=36m=12
P 3 r , 4 w , 5 b = C 12 3 × C 12 4 × C 12 5 C 36 12 = 12 ! 3 ! × ( 12 − 3 ) ! × 12 ! 4 ! × ( 12 − 4 ) ! × 12 ! 5 ! × ( 12 − 5 ) ! 36 ! 12 ! × ( 36 − 12 ) ! = 12 ! 3 ! × 9 ! × 12 ! 4 ! × 8 ! × 12 ! 5 ! × 7 ! 36 ! 12 ! × 24 ! = 12 ! 3 ! × ( 12 − 3 ) ! × 12 ! 4 ! × ( 12 − 4 ) ! × 12 ! 5 ! × ( 12 − 5 ) ! 36 ! 12 ! × ( 36 − 12 ) ! = 479 , 001 , 600 6 × 362 , 880 × 479 , 001 , 600 24 × 40 , 320 × 479 , 001 , 600 120 × 5 , 040 371 , 993 , 326 , 789 , 901 , 217 , 467 , 999 , 448 , 150 , 835 , 200 , 000 , 000 479 , 001 , 600 × 620 , 448 , 401 , 733 , 239 , 439 , 360 , 000 = 220 × 495 × 792 1 , 251 , 677 , 700 = 86 , 248 , 800 1 , 251 , 677 , 700 = 95832 1390753 = 0.06890655637629399325401419231165 \begin{equation} \begin{split} P_{3_r,4_w,5_b} &= \frac{C_{12}^{3} \times C_{12}^{4} \times C_{12}^{5} }{C_{36}^{12}} \\ &=\frac{\frac{12!}{3!\times (12-3)!}\times \frac{12!}{4!\times (12-4)!} \times\frac{12!}{5!\times (12-5)!}}{\frac{36!}{12!\times (36-12)!}} \\ &=\frac{\frac{12!}{3!\times 9!}\times \frac{12!}{4!\times 8!} \times\frac{12!}{5!\times 7!}}{\frac{36!}{12!\times 24!}} \\ &=\frac{\frac{12!}{3!\times (12-3)!}\times \frac{12!}{4!\times (12-4)!} \times\frac{12!}{5!\times (12-5)!}}{\frac{36!}{12!\times (36-12)!}} \\ &=\frac{\frac{479,001,600}{6\times 362,880}\times \frac{479,001,600}{24\times 40,320} \times\frac{479,001,600}{120\times 5,040}}{\frac{371,993,326,789,901,217,467,999,448,150,835,200,000,000}{479,001,600\times 620,448,401,733,239,439,360,000}} \\ &=\frac{ 220 \times 495 \times 792}{1,251,677,700} \\ &=\frac{86,248,800}{1,251,677,700} \\ &=\frac{95832}{1390753} \\ &=0.06890655637629399325401419231165 \end{split} \end{equation} P3r,4w,5b=C3612C123×C124×C125=12!×(36−12)!36!3!×(12−3)!12!×4!×(12−4)!12!×5!×(12−5)!12!=12!×24!36!3!×9!12!×4!×8!12!×5!×7!12!=12!×(36−12)!36!3!×(12−3)!12!×4!×(12−4)!12!×5!×(12−5)!12!=479,001,600×620,448,401,733,239,439,360,000371,993,326,789,901,217,467,999,448,150,835,200,000,0006×362,880479,001,600×24×40,320479,001,600×120×5,040479,001,600=1,251,677,700220×495×792=1,251,677,70086,248,800=139075395832=0.06890655637629399325401419231165
1.2 题目2
袋中共 36 36 36 个球, 红 \fcolorbox{red}{#FADADE}{\color{red}{红}} 红 白 \fcolorbox{white}{#808080}{\color{white}{白}} 白 黑 \fcolorbox{#808080}{#0D0D0D}{\color{#808080}{黑}} 黑各 12 12 12个,问能一次抽到颜色组合是 3 3 3、 4 4 4、 5 5 5 的概率是多少?
P m 1 , m 2 , m 3 = N ! N 1 ! × N 2 ! × … × N k ! × C n 1 m 1 × C n 2 m 2 × C n 3 m 3 C n m P_{m_1,m_2,m_3} =\frac{N!}{N_1! \times N_2! \times \ldots \times N_k!} \times \frac{C_{n_1}^{m_1} \times C_{n_2}^{m_2} \times C_{n_3}^{m_3} }{C_{n}^{m}} Pm1,m2,m3=N1!×N2!×…×Nk!N!×CnmCn1m1×Cn2m2×Cn3m3
其中:
P m 1 , m 2 , m 3 P_{m_1,m_2,m_3} Pm1,m2,m3 – 概率
C n m C_{n}^{m} Cnm – n个球中,抽m个球的组合数。
C n 1 m 1 C_{n_1}^{m_1} Cn1m1 – n 1 n_1 n1个颜色1球中,抽 m 1 m_1 m1个颜色1球的组合数。
C n 2 m 2 C_{n_2}^{m_2} Cn2m2 – n 2 n_2 n2个颜色2球中,抽 m 2 m_2 m2个颜色2球的组合数。
C n 3 m 3 C_{n_3}^{m_3} Cn3m3 – n 3 n_3 n3个颜色3球中,抽 m 3 m_3 m3个颜色3球的组合数。
N ! N 1 ! × N 2 ! × … × N k ! \frac{N!}{N_1! \times N_2! \times \ldots \times N_k!} N1!×N2!×…×Nk!N! – m 1 、 m 2 、 m 3 m_1、m_2、m_3 m1、m2、m3数的排列组合数。
m 1 、 m 2 、 m 3 m_1、m_2、m_3 m1、m2、m3 | N ! N 1 ! × N 2 ! × … × N k ! \frac{N!}{N_1! \times N_2! \times \ldots \times N_k!} N1!×N2!×…×Nk!N! | 排列组合 |
---|---|---|
3,4,5 | 第1个数3出现1次 第2个数4出现1次 第3个数5出现1次 3 ! 1 ! × 1 ! × 1 ! \frac{3!}{1! \times 1!\times 1!} 1!×1!×1!3! | 3,4,5 3,5,4 4,3,5 4,5,3 5,4,3 5,3,4 |
4,4,4 | 第1个数4出现3次 3 ! 3 ! \frac{3!}{3!} 3!3! | 4,4,4 |
3,3,6 | 第1个数3出现2次 第2个数6出现1次 3 ! 2 ! × 1 ! \frac{3!}{2! \times 1!} 2!×1!3! | 3,3,6 3,6,3 6,3,3 |
1,1,10 | 第1个数1出现2次 第2个数10出现1次 3 ! 2 ! × 1 ! \frac{3!}{2! \times 1!} 2!×1!3! | 1,1,10 1,10,1 10,1,1 |
… \ldots … | … \ldots … | … \ldots … |
根据已知条件代入公式:
已知条件
{
n
1
=
12
n
2
=
12
n
3
=
12
m
1
=
3
m
2
=
4
m
3
=
5
n
=
36
m
=
12
N
=
3
N
1
(
3
)
=
1
N
2
(
4
)
=
1
N
3
(
5
)
=
1
已知条件 \begin{cases} n_1=12 \\ n_2=12 \\ n_3=12 \\ m_1=3 \\ m_2=4 \\ m_3=5 \\ n=36 \\ m=12 \\ N=3\\ N_{1(3)}=1\\ N_{2(4)}=1\\ N_{3(5)}=1\\ \end{cases}
已知条件⎩
⎨
⎧n1=12n2=12n3=12m1=3m2=4m3=5n=36m=12N=3N1(3)=1N2(4)=1N3(5)=1
P 3 , 4 , 5 = 3 ! 1 ! × 1 ! × 1 ! × C 12 3 × C 12 4 × C 12 5 C 36 12 = 6 1 × 12 ! 3 ! × ( 12 − 3 ) ! × 12 ! 4 ! × ( 12 − 4 ) ! × 12 ! 5 ! × ( 12 − 5 ) ! 36 ! 12 ! × ( 36 − 12 ) ! = 6 × 12 ! 3 ! × 9 ! × 12 ! 4 ! × 8 ! × 12 ! 5 ! × 7 ! 36 ! 12 ! × 24 ! = 6 × 12 ! 3 ! × ( 12 − 3 ) ! × 12 ! 4 ! × ( 12 − 4 ) ! × 12 ! 5 ! × ( 12 − 5 ) ! 36 ! 12 ! × ( 36 − 12 ) ! = 6 × 479 , 001 , 600 6 × 362 , 880 × 479 , 001 , 600 24 × 40 , 320 × 479 , 001 , 600 120 × 5 , 040 371 , 993 , 326 , 789 , 901 , 217 , 467 , 999 , 448 , 150 , 835 , 200 , 000 , 000 479 , 001 , 600 × 620 , 448 , 401 , 733 , 239 , 439 , 360 , 000 = 6 × 220 × 495 × 792 1 , 251 , 677 , 700 = 6 × 86 , 248 , 800 1 , 251 , 677 , 700 = 6 × 95832 1390753 = 574992 1390753 = 0.41343933825776395952408515386988 \begin{equation} \begin{split} P_{3,4,5} &=\frac{3!}{1! \times 1!\times 1!}\times \frac{C_{12}^{3} \times C_{12}^{4} \times C_{12}^{5} }{C_{36}^{12}} \\ &=\frac{6}{1}\times\frac{\frac{12!}{3!\times (12-3)!}\times \frac{12!}{4!\times (12-4)!} \times\frac{12!}{5!\times (12-5)!}}{\frac{36!}{12!\times (36-12)!}} \\ &=6\times \frac{\frac{12!}{3!\times 9!}\times \frac{12!}{4!\times 8!} \times\frac{12!}{5!\times 7!}}{\frac{36!}{12!\times 24!}} \\ &=6\times \frac{\frac{12!}{3!\times (12-3)!}\times \frac{12!}{4!\times (12-4)!} \times\frac{12!}{5!\times (12-5)!}}{\frac{36!}{12!\times (36-12)!}} \\ &=6\times \frac{\frac{479,001,600}{6\times 362,880}\times \frac{479,001,600}{24\times 40,320} \times\frac{479,001,600}{120\times 5,040}}{\frac{371,993,326,789,901,217,467,999,448,150,835,200,000,000}{479,001,600\times 620,448,401,733,239,439,360,000}} \\ &=6\times \frac{ 220 \times 495 \times 792}{1,251,677,700} \\ &=6\times \frac{86,248,800}{1,251,677,700} \\ &=6\times \frac{95832}{1390753} \\ &=\frac{ 574992}{1390753} \\ &=0.41343933825776395952408515386988 \end{split} \end{equation} P3,4,5=1!×1!×1!3!×C3612C123×C124×C125=16×12!×(36−12)!36!3!×(12−3)!12!×4!×(12−4)!12!×5!×(12−5)!12!=6×12!×24!36!3!×9!12!×4!×8!12!×5!×7!12!=6×12!×(36−12)!36!3!×(12−3)!12!×4!×(12−4)!12!×5!×(12−5)!12!=6×479,001,600×620,448,401,733,239,439,360,000371,993,326,789,901,217,467,999,448,150,835,200,000,0006×362,880479,001,600×24×40,320479,001,600×120×5,040479,001,600=6×1,251,677,700220×495×792=6×1,251,677,70086,248,800=6×139075395832=1390753574992=0.41343933825776395952408515386988
2、枚举法
使用程序模拟每一次抽取,计算每一步的概率,并进行累计汇总。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using static GL39_9.Program;
namespace GL39_9
{
internal class Program
{
/// <summary>
/// 汇总类型
/// </summary>
public enum eSummaryType
{
/// <summary>
/// 按数量汇总
/// </summary>
Number,
/// <summary>
/// 按名称和数量汇总
/// P(36,12(3,4,5))=N! * C(12,3)*C(12,4)*C(12,5)/C(36,12)
/// C(12,3) = 12!/(3!*(12-3)!) = 12!/(3!*9!)
/// C(12,4) = 12!/(4!*(12-4)!) = 12!/(4!*8!)
/// C(12,4) = 12!/(5!*(12-5)!) = 12!/(5!*7!)
/// 3 4 5 (N=3)
/// 4 4 4 (N=1)
/// 3 3 6 (N=2)
/// </summary>
NameNumber
}
public static double Calc(int[] keys,int pre=12)
{
int nType = keys.GroupBy(o=>o).Count();
BigInteger FT = Factorial(nType);
int t = pre * keys.Count();
int c = keys.Sum(o=>o);
BigInteger fz = 1;
foreach (var item in keys)
{
fz *= C(pre, item);
}
BigInteger fm = C(t,c);
return ((double)(long)FT * ((double)(long)fz / (double)(long)fm));
}
public static BigInteger C(int n,int m)
{
var fn = Factorial(n);
var fm = Factorial(m);
var fmn = Factorial(n-m);
return fn /(fm * fmn);
}
public static BigInteger Factorial(int n)
{
if (n < 0) throw new ArgumentException("n must be non-negative.");
if (n == 0) return 1;
return n * Factorial(n - 1);
}
/// <summary>
/// 暗盒/袋中的物品信息
/// </summary>
public class CBlackBoxItem
{
/// <summary>
/// 物品唯一编号
/// </summary>
public int Id { get; set; }
/// <summary>
/// 物品名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 物品数量
/// </summary>
public int Number { get; set; }
/// <summary>
///
/// </summary>
/// <param name="id">物品唯一编号</param>
/// <param name="name">物品名称</param>
/// <param name="number">物品数量</param>
public CBlackBoxItem(int id, string name, int number)
{
Id = id;
Name = name;
Number = number;
}
/// <summary>
/// 物品信息
/// </summary>
/// <returns></returns>
public override string ToString()
{
return $"{Id}.{Name} {Number}个";
}
/// <summary>
/// 复制物品信息及数量
/// </summary>
/// <returns></returns>
public CBlackBoxItem Copy()
{
return new CBlackBoxItem(Id, Name, Number);
}
/// <summary>
/// 复制物品并减去指定数量的物品
/// </summary>
/// <param name="val"></param>
/// <returns></returns>
public CBlackBoxItem CopySub(int val)
{
return new CBlackBoxItem(Id, Name, Number - val);
}
/// <summary>
/// 复制物品并设置数量
/// </summary>
/// <param name="val"></param>
/// <returns></returns>
public CBlackBoxItem Copy(int val)
{
return new CBlackBoxItem(Id, Name, val);
}
}
/// <summary>
/// 轮次信息
/// </summary>
public class CRoundInformation
{
/// <summary>
/// 前一轮的信息
/// </summary>
public CRoundInformation previousRound = null;
/// <summary>
/// 本轮暗箱信息
/// </summary>
public List<CBlackBoxItem> CurrentRoundBlackBox = new List<CBlackBoxItem> {
new CBlackBoxItem(1,"红色球/棍子",9),
new CBlackBoxItem(2,"绿色球/棍子",9),
new CBlackBoxItem(3,"蓝色球/棍子",9),
};
/// <summary>
/// 从暗盒中获取总数量
/// </summary>
/// <returns></returns>
public int GetTotalNumberFromBlackBox()
{
return CurrentRoundBlackBox.Select(o => o.Number).Sum();
}
/// <summary>
/// 当前从暗箱中抽中的物品
/// </summary>
public CBlackBoxItem CurrentDrawnBlackBoxItem = null;
/// <summary>
/// 当前轮次概率
/// </summary>
public double CurrentRoundProbability = 0;
/// <summary>
/// 累计所有轮的概率
/// </summary>
public double CumulativeProbability = 0;
/// <summary>
/// 累计所有抽到的物品
/// </summary>
public List<CBlackBoxItem> CumulativeDrawnBlackBoxItems = new List<CBlackBoxItem>();
/// <summary>
/// 所有抽到的物品归类信息
/// </summary>
public string SummaryInformation = "";
/// <summary>
/// 轮次
/// </summary>
public int RoundIndex = 0;
/// <summary>
/// 更新累计的概率
/// </summary>
public void UpdateCumulativeProbability()
{
double probability = 1.0;
GetCumulativeProbability(this, ref probability);
CumulativeProbability = probability;
}
/// <summary>
/// 递归获取累计概率
/// </summary>
/// <param name="roundInformation"></param>
/// <param name="probability"></param>
private void GetCumulativeProbability(CRoundInformation roundInformation, ref double probability)
{
if (roundInformation == null) return;
if (roundInformation.RoundIndex <= 0) return;
probability *= roundInformation.CurrentRoundProbability;
GetCumulativeProbability(roundInformation.previousRound, ref probability);
}
/// <summary>
/// 统计抽到的物品,获取汇总信息
/// </summary>
public void UpdateDrawnBlackBoxItems(eSummaryType summaryType = eSummaryType.Number)
{
List<CBlackBoxItem> blackBoxItems = new List<CBlackBoxItem>();
GetDrawnBlackBoxItems(this, ref blackBoxItems);
CumulativeDrawnBlackBoxItems = blackBoxItems;
List<CBlackBoxItem> temps = new List<CBlackBoxItem>();
foreach (var item in CurrentRoundBlackBox)
{
temps.Add(item.Copy(blackBoxItems.Where(o => o.Id == item.Id).Count()));
}
temps = temps.OrderByDescending(o => o.Number).ToList();
if(summaryType == eSummaryType.Number)
{
SummaryInformation = string.Join(":", temps.Select(o => o.Number));
}
else if (summaryType == eSummaryType.NameNumber)
{
SummaryInformation = string.Join(":", temps.Select(o => o.Number)) +"|" + string.Join(":", temps);
}
}
/// <summary>
/// 递归获取每一轮的物品
/// </summary>
/// <param name="roundInformation"></param>
/// <param name="blackBoxItems"></param>
private void GetDrawnBlackBoxItems(CRoundInformation roundInformation, ref List<CBlackBoxItem> blackBoxItems)
{
if (roundInformation == null) return;
if (roundInformation.RoundIndex <= 0) return;
blackBoxItems.Add(roundInformation.CurrentDrawnBlackBoxItem);
GetDrawnBlackBoxItems(roundInformation.previousRound, ref blackBoxItems);
}
/// <summary>
/// 从暗盒中抽一个物品
/// </summary>
/// <param name="roundIndex"></param>
/// <returns></returns>
public List<CRoundInformation> DrawnOneItemFromBlackBox(int roundIndex)
{
List<CRoundInformation> roundInformations = new List<CRoundInformation>();
var total = GetTotalNumberFromBlackBox();
foreach (var item in CurrentRoundBlackBox)
{
if (item.Number <= 0)
{
continue;
}
CRoundInformation roundInformation = new CRoundInformation();
roundInformation.RoundIndex = roundIndex;
roundInformation.previousRound = this;
roundInformation.CurrentDrawnBlackBoxItem = new CBlackBoxItem(item.Id, item.Name, 1);
roundInformation.CurrentRoundProbability = item.Number / (double)total;
roundInformation.CurrentRoundBlackBox.Clear();
foreach (var itme in CurrentRoundBlackBox)
{
if (item.Id == itme.Id)
{
roundInformation.CurrentRoundBlackBox.Add(itme.CopySub(1));
}
else
{
roundInformation.CurrentRoundBlackBox.Add(itme.Copy());
}
}
roundInformations.Add(roundInformation);
}
return roundInformations;
}
}
/// <summary>
/// 概率小游戏
/// </summary>
public class CProbabilityGame
{
public List<CRoundInformation> Source = new List<CRoundInformation>();
public int DrawnTimes = 9;
/// <summary>
/// 每次抽取花费
/// 100块抽5次
/// </summary>
public double SpendEveryTime = 20;
public eSummaryType SummaryType = eSummaryType.Number;
/// <summary>
/// 奖池
/// </summary>
public Dictionary<string, double> Jackpots = new Dictionary<string, double>
{
{"9:0:0",100},//: 0.000640092173272951‰
{"8:1:0",30},//: 0.103694932070218‰
{"7:2:0",10},//: 1.65911891312349‰
{"7:1:1",20},//: 1.86650877726393‰
{"6:3:0",10},//: 9.03298074922795‰
{"6:2:1",10},//: 34.8414971755928‰
{"5:4:0",2},//: 20.3242066857629‰
{"5:3:1",2},//: 121.945240114572‰
{"5:2:2",1},//: 104.524491526778‰
{"4:4:1",1},//: 91.4589300859336‰
{"4:3:2",-10},//: 487.780960458368‰
{"3:3:3",1}//: 126.461730489187‰
};
public CProbabilityGame()
{
//Init312Drawn12();
Init39Drawn9();
}
/// <summary>
/// 3 种颜色的物品 每种 12个,抽12个
/// </summary>
public void Init312Drawn12()
{
DrawnTimes = 12;
SummaryType = eSummaryType.Number;
var item = new CRoundInformation();
item.CurrentRoundBlackBox.Clear();
item.CurrentRoundBlackBox = new List<CBlackBoxItem> {
new CBlackBoxItem(1,"红色球/棍子",DrawnTimes),
new CBlackBoxItem(2,"绿色球/棍子",DrawnTimes),
new CBlackBoxItem(3,"蓝色球/棍子",DrawnTimes),
};
Source.Add(item);
Jackpots.Clear();
Jackpots = new Dictionary<string, double>
{
{"12:0:0",100},//2.39678313354948E-06‰
{"11:1:0",30},//0.00069027354246225‰
{"10:2:0",20},//0.020880774659483‰
{"10:1:1",20},//0.0227790269012542‰
{"9:3:0",10},//0.232008607327592‰
{"9:2:1",10},//0.835230986379351‰
{"8:4:0",5},//1.17454357459599‰
{"8:3:1",5},//6.26423239784602‰
{"8:2:2",5},//5.16799172822235‰
{"7:5:0",5},//3.0068315509653‰
{"7:4:1",2},//22.551236632241‰
{"7:3:2",2},//55.1252451009996‰
{"6:6:0",5},//2.04631591662927‰
{"6:5:1",2},//42.0956417134937‰
{"6:4:2",1},//144.70376839016‰
{"6:3:3",1},//107.187976585368‰
{"5:5:2",1},//99.2254411818329‰
{"5:4:3",-10},//413.439338257532‰
{"4:4:4",1},//96.8998449041164‰
};
}
/// <summary>
/// 3 种颜色的物品 每种 9个,抽9个
/// </summary>
public void Init39Drawn9()
{
DrawnTimes = 9;
SpendEveryTime = 0.5;
SummaryType = eSummaryType.Number;
var item = new CRoundInformation();
item.CurrentRoundBlackBox.Clear();
item.CurrentRoundBlackBox = new List<CBlackBoxItem> {
new CBlackBoxItem(1,"红色球/棍子",DrawnTimes),
new CBlackBoxItem(2,"绿色球/棍子",DrawnTimes),
new CBlackBoxItem(3,"蓝色球/棍子",DrawnTimes),
};
Source.Add(item);
Jackpots.Clear();
Jackpots = new Dictionary<string, double>
{
{"9:0:0",100},//: 0.000640092173272951‰
{"8:1:0",30},//: 0.103694932070218‰
{"7:2:0",10},//: 1.65911891312349‰
{"7:1:1",20},//: 1.86650877726393‰
{"6:3:0",10},//: 9.03298074922795‰
{"6:2:1",10},//: 34.8414971755928‰
{"5:4:0",2},//: 20.3242066857629‰
{"5:3:1",2},//: 121.945240114572‰
{"5:2:2",1},//: 104.524491526778‰
{"4:4:1",1},//: 91.4589300859336‰
{"4:3:2",-10},//: 487.780960458368‰
{"3:3:3",1}//: 126.461730489187‰
};
}
/// <summary>
/// 计算
/// </summary>
/// <returns></returns>
public double Calculate()
{
List<CRoundInformation> listTemp = new List<CRoundInformation>();
Drawn(ref listTemp);
Cumulative(ref listTemp);
var pro = SummaryProbabilitys(listTemp);
if(CheckProbabilitys(pro))
{
return CalculateProfitLoss(pro);
}
return double.MinValue;
}
/// <summary>
/// 检查概率是否归一
/// </summary>
/// <param name="Probabilitys"></param>
/// <returns></returns>
public bool CheckProbabilitys(Dictionary<string, double> Probabilitys)
{
var val = Probabilitys.Sum(o => o.Value);
System.Diagnostics.Debug.WriteLine(val);
if(val>1-0.0000001 && val<1+0.0000001)
{
return true;
}
return false;
}
/// <summary>
/// 累计
/// </summary>
/// <param name="listTemp"></param>
private void Cumulative(ref List<CRoundInformation> listTemp)
{
foreach (var item in listTemp)
{
item.UpdateCumulativeProbability();
item.UpdateDrawnBlackBoxItems(SummaryType);
}
}
/// <summary>
/// 抽取
/// </summary>
/// <param name="listTemp"></param>
private void Drawn(ref List<CRoundInformation> listTemp)
{
listTemp = Source;
for (int i = 1; i <= DrawnTimes; i++)
{
var temps = new List<CRoundInformation>();
foreach (var item in listTemp)
{
if (item.RoundIndex < i - 1)
{
continue;
}
var list = item.DrawnOneItemFromBlackBox(i);
temps.AddRange(list);
}
listTemp = temps;
}
}
/// <summary>
/// 汇总
/// </summary>
/// <param name="listTemp"></param>
/// <returns></returns>
private Dictionary<string, double> SummaryProbabilitys(List<CRoundInformation> listTemp)
{
Dictionary<string, double> Probabilitys = new Dictionary<string, double>();
System.Diagnostics.Debug.WriteLine($"|组合|概率");
System.Diagnostics.Debug.WriteLine($"|--|--|");
foreach (var item in listTemp.GroupBy(o => o.SummaryInformation).OrderByDescending(o => o.Key))
{
var glall = item.Sum(o => o.CumulativeProbability);
Probabilitys.Add(item.Key, glall);
System.Diagnostics.Debug.WriteLine($"|{item.Key}|{glall * 100}%");
//System.Diagnostics.Debug.WriteLine($"{{\"{item.Key}\",{1.0/ glall}}},//{glall*1000}‰");
}
return Probabilitys;
}
/// <summary>
/// 计算盈亏
/// </summary>
private double CalculateProfitLoss(Dictionary<string, double> Probabilitys)
{
double revenue = -SpendEveryTime;
foreach (var item in Jackpots)
{
if (Probabilitys.ContainsKey(item.Key))
{
revenue += Probabilitys[item.Key] * item.Value;
}
}
return revenue;
}
static void Main(string[] args)
{
CProbabilityGame game = new CProbabilityGame();
var item = Program.Calc(new int[3] {3,4,5 },12);
System.Diagnostics.Debug.WriteLine(item);
var val = 20 * game.Calculate();
System.Diagnostics.Debug.WriteLine(val);
}
}
}
}
3、拓展
街头玩法
每次费用
10
/
20
=
0.5
元
10/20=0.5元
10/20=0.5元
根据概率,抽一次罚
3.7749845577769543
元
3.7749845577769543元
3.7749845577769543元
加上成本
0.5
0.5
0.5 ,每把亏
4.2749845577769543
元
4.2749845577769543元
4.2749845577769543元,
每次交费玩
20
20
20次,一次缴费
4.2749845577769543
×
20
=
85.499691155539086
元
4.2749845577769543\times 20=85.499691155539086元
4.2749845577769543×20=85.499691155539086元
常见玩法概率表
3种颜色物品,每种9个,每次抽9个的概率
组合 | 概率 |
---|---|
9:0:0 | 0.0000640092173272951% |
8:1:0 | 0.0103694932070218% |
7:2:0 | 0.165911891312349% |
7:1:1 | 0.186650877726393% |
6:3:0 | 0.903298074922795% |
6:2:1 | 3.48414971755928% |
5:4:0 | 2.03242066857629% |
5:3:1 | 12.1945240114572% |
5:2:2 | 10.4524491526778% |
4:4:1 | 9.14589300859336% |
4:3:2 | 48.7780960458368% |
3:3:3 | 12.6461730489187% |
3种颜色物品,每种12个,每次抽12个的概率
组合 | 概率 |
---|---|
12:0:0 | 0.000000239678313354948% |
11:1:0 | 0.000069027354246225% |
10:2:0 | 0.0020880774659483% |
10:1:1 | 0.00227790269012542% |
9:3:0 | 0.0232008607327592% |
9:2:1 | 0.0835230986379351% |
8:4:0 | 0.117454357459599% |
8:3:1 | 0.626423239784602% |
8:2:2 | 0.516799172822235% |
7:5:0 | 0.30068315509653% |
7:4:1 | 2.2551236632241% |
7:3:2 | 5.51252451009996% |
6:6:0 | 0.204631591662927% |
6:5:1 | 4.20956417134937% |
6:4:2 | 14.470376839016% |
6:3:3 | 10.7187976585368% |
5:5:2 | 9.92254411818329% |
5:4:3 | 41.3439338257532% |
4:4:4 | 9.68998449041164% |