zoukankan      html  css  js  c++  java
  • C# 做一个指定概率的抽奖程序

            static void Main(string[] args)
            {
                //各物品的概率保存在数组里
                float[] area = new float[4]{
                    0.980f,
                    0.550f,
                    0.230f,
                    0.010f
                };
    
                //单次测试
                //Console.WriteLine(Get(area));
    
                //批量测试
                int[] result = new int[4]{
                    0,
                    0,
                    0,
                    0
                };
                for (int i = 0; i < 1770000; i++)    //为了比对结果方便,这里循环的次数是总概率的1000倍
                {
                    int n = Get(area);      //本次抽奖结果
                    result[n]++;            //统计抽到的次数
                }
                Console.WriteLine("结果:");
                foreach (int times in result)
                {
                    Console.WriteLine(times);
                }
            }
    
            /// <summary>
            /// 获取抽奖结果
            /// </summary>
            /// <param name="prob">各物品的抽中概率</param>
            /// <returns>返回抽中的物品所在数组的位置</returns>
            private static int Get(float[] prob)
            {
                int result = 0;
                int n = (int)(prob.Sum() * 1000);           //计算概率总和,放大1000倍
                Random r = rnd;
                float x = (float)r.Next(0, n) / 1000;       //随机生成0~概率总和的数字
    
                for (int i = 0; i < prob.Count(); i++)
                {
                    float pre = prob.Take(i).Sum();         //区间下界
                    float next = prob.Take(i + 1).Sum();    //区间上界
                    if (x >= pre && x < next)               //如果在该区间范围内,就返回结果退出循环
                    {
                        result = i;
                        break;
                    }
                }
                return result;
            }
    
            private static Random rnd = new Random();

     

  • 相关阅读:
    document
    reg() replace
    BOM和DOM的区别
    注册表
    实现移动端通过下拉菜单栏实现pc端的导航栏
    通过ajax获取api,并且通过jquery获取自定义属性
    git的使用
    当盒子不设置width,而设置max-width遇到的问题
    CSS之position
    JavaScript之数组常用的方法
  • 原文地址:https://www.cnblogs.com/linji/p/3976305.html
Copyright © 2011-2022 走看看