zoukankan      html  css  js  c++  java
  • C#抽奖算法

    摘自网络

    static void Main(string[] args)
            {
                //各物品的概率保存在数组里
                float[] area = new float[4]{
                    0.5f,
                    0.5f,
                    0,
                    0
                };
    
                //单次测试
                //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);
                }
                Console.ReadLine();
            }
    
            /// <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 = new Random();
                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;
            }

  • 相关阅读:
    Object-C 声明属性为什么用下划线,代码规范和编程风格
    iOS API 概述
    iOS 彻底学会使用delegate
    iOS NSNotification的使用
    L1_6 连续因子
    天梯 L1_46整除光棍
    51-Nod 1279
    UVA
    hdu 1078
    Poj 1088 滑雪 递归实现
  • 原文地址:https://www.cnblogs.com/Jerrycjc/p/5773654.html
Copyright © 2011-2022 走看看