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

    1

    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();

    2

  • 相关阅读:
    简单的php socket 实例
    正则表达式语法
    js 固话正则
    php性能剖析的几款软件
    mysql基准测试
    mysql事务
    PHP字符串函数
    Git的使用---4.git的基本使用
    Git的使用---3.git的配置
    Git的使用---2.git的安装
  • 原文地址:https://www.cnblogs.com/netlock/p/15671344.html
Copyright © 2011-2022 走看看