美文网首页
概率算法

概率算法

作者: forsek | 来源:发表于2017-10-18 11:43 被阅读0次
    一. 生成随机数(可设定范围)

    使用C#自带类System.Radom(int seed),来生成随机数,
    使用Guid.NewGuid().GetHashCode(),生成新的Guid,然后获取它的Hash Code(int),作为种子放入System.Radom(int seed)使用。
    种子作用是使每次生成的数字分布更加随机。
    所有的随机数生成算法都是伪随机数。

    二. 使用for循环,循环生成随机数,然后划分范围(概率分配),对落入不同范围内的随机数进行计数,最后计算出实际概率值。
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            static int randomNum;
            static int awards;
            static int a = 0;
            static int b = 0;
            static int c = 0;
            static int d = 0;
            static int e = 0;
            static int f = 0;
            static int g = 0;
            static int h = 0;
            static int ii = 0;
            static int j = 0;
            static int k = 0;
            static int l = 0;
            //添加整形变量为循环次数与概率分母,更改循环次数测试时,只要修改一个变量,即可得出正确概率
            static int Cnum = 10000;
            static void Main(string[] args)
            {   //循环次数更换为变量
                for (int i = 0; i < Cnum; i++)
                {
    
                    string strprize = "";
                    int payscore = 0;
                    int itemid = 32;
    
                    awards = new System.Random(Guid.NewGuid().GetHashCode()).Next(1, 1001);//抽奖概率随机数
                    if (awards < 2) //1等奖
                    {
                        strprize = "1等奖";
                        payscore = 100;
                        a = a + 1;
    
                    }
                    else if (awards < 6)
                    {
                        strprize = "2等奖";
                        payscore = 50;
                        b = b + 1;
                    }
                    else if (awards < 11)
                    {
                        strprize = "3等奖";
                        payscore = 40;
                        c = c + 1;
    
                    }
                    else if (awards < 51)
                    {
                        strprize = "4等奖";
                        payscore = 20;
                        d = d + 1;
    
                    }
                    else if (awards < 125)
                    {
                        strprize = "5等奖";
                        payscore = 5;
    
                        e = e + 1;
                    }
                    else if (awards < 201)
                    {
                        strprize = "6等奖";
                        payscore = 1;
                        itemid = 46;
    
                        f = f + 1;
                    }
                    else
                    {
    
                        randomNum = new System.Random(Guid.NewGuid().GetHashCode()).Next(1, 7);
    
                        if (randomNum == 1)
                        {
                            strprize = "7等奖";
                            payscore = 0;
    
                            g = g + 1;
                        }
                        else if (randomNum == 2)
                        {
                            strprize = "7等奖";
                            payscore = 1;
                            itemid = 46;
    
                            h = h + 1;
                        }
                        else if (randomNum == 3)
                        {
                            strprize = "7等奖";
                            payscore = 0;
                            ii = ii + 1;
    
                        }
                        else if (randomNum == 4)
                        {
                            strprize = "7等奖";
                            payscore = 10;
                            j = j + 1;
    
    
                        }
                        else if (randomNum == 5)
                        {
                            strprize = "7等奖";
                            payscore = 1;
                            itemid = 47;
                            j = j + 1;
    
                        }
                        else if (randomNum == 6)
                        {
                            strprize = "7等奖";
                            payscore = 1;
                            itemid = 47;
    
                            j = j + 1;
                        }
                    }
    
    
                }
                //隐式转换时,要先把Cnum转换成double类型,然后除法的结果才能隐式转换为double
                double cnum = Convert.ToDouble(Cnum);
    
                Console.Write("1等奖:    " + (a / cnum).ToString("0.####%") +"    次数"+a+ "\n");
                Console.Write("2等奖:   " + (b / cnum).ToString("0.####%") + "    次数" + b + "\n");
                Console.Write("3等奖:   " + (c / cnum).ToString("0.####%") + "    次数" + c + "\n");
                Console.Write("4等奖:   " + (d / cnum).ToString("0.####%") + "    次数" + d + "\n");
                Console.Write("5等奖:    " + (e / cnum).ToString("0.####%") + "    次数" + e + "\n");
                Console.Write("6等奖:    " + (f / cnum).ToString("0.####%") + "    次数" + f + "\n");
                Console.Write("7等奖:    " + (g / cnum).ToString("0.####%") + "    次数" + g + "\n");
                Console.Write("7等奖:    " + (ii / cnum).ToString("0.####%") + "    次数" + ii + "\n");
                Console.Write("7等奖:   " + (h / cnum).ToString("0.####%") + "    次数" + h + "\n");
                Console.Write("7等奖:    " + (j / cnum).ToString("0.####%") + "    次数" + j + "\n");
                Console.ReadLine();
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:概率算法

          本文链接:https://www.haomeiwen.com/subject/jbvsuxtx.html