clc syms r format long s=0; n=10; w=[-1,-1,-1,-1,-1,1,17,179,1299,2599,24999]; % for k=0: 10 % s=s+ nchoosek(20,k)*nchoosek(60,n-k)/nchoosek(80,n); % end % s for k=0: 10 nchoosek(20,k)*nchoosek(60,n-k)/nchoosek(80,n) end
static void T13() { var dic = new List<int>(); for (int i = 0; i < 80; i++) { dic.Add(i); } var right = new List<int>(){0,0,0,0,0,0,0,0,0,0,0}; var rnd = new Random(Environment.TickCount); var total = 1000000; //庄家选20 var dic20 = new List<int>(); for (int i = 0; i < 20; i++) { var index = rnd.Next(80); while (dic20.Contains(dic[index])) { index = rnd.Next(80); } dic20.Add(dic[index]); } for (int j = 0; j < total; j++) { #region 放在内外没结果一样 //var dic20 = new List<int>(); ////庄家选20 //for (int i = 0; i < 20; i++) //{ // var index = rnd.Next(80); // while (dic20.Contains(dic[index])) // { // index = rnd.Next(80); // } // dic20.Add(dic[index]); //} #endregion #region 模拟 var sb = new StringBuilder(); //赌徒选10 var dic10 = new List<int>(); for (int i = 0; i < 10; i++) { var index = rnd.Next(80); while (dic10.Contains(dic[index])) { index = rnd.Next(80); } dic10.Add(dic[index]); } var list= dic10.Intersect(dic20); right[list.Count()]++; #endregion } //输出结果 for (int i = 0; i <= 10; i++) { var it = right[i]; Console.WriteLine("P(X={0})={1}", i, (double)it / (double)total); } }