C# 抢红包生成 抽奖概率,红包分配

抽奖概率:

   public int PrabalityRandom()
        {
            Random random = new Random();
            int i= Convert.ToInt32(AwardRate);
            if (random.Next(100) < i) //百分之五十的几率中奖
            {
                return 1;//表示中奖
            }
            else
            {
                return 0;//表示不中奖
            }
        }
 

生成红包:

       private void button2_Click_1(object sender, EventArgs e)
        {

            if (btnOkOut.Text.Equals("开启红包"))
            {
                MessageBox.Show("请您先点击开启红包按钮!","系统提示!");
                return;
            }
            string Probability = cbxProbability.DisplayMember;//中奖概率
            if (txtWinner.Text.Trim() == "")
            {
                MessageBox.Show("人数不能为空");
                return;
            }
            if (txtWinner.Text.Trim() == "")
            {
                MessageBox.Show("中奖金额");
                return;
            }
            int Winner = Convert.ToInt32(txtWinner.Text);//人数
            int Money = Convert.ToInt32(txtMoney.Text) * 100;//中奖金额
            DataSet ds = DbHelperSQL.Query("SELECT * from Data_RedEnvelopeAward");
            if (ds.Tables[0].Rows.Count != 0)//truncate table Data_RedEnvelopeAward
            {
                DbHelperSQL.ExecuteSql("truncate table Data_RedEnvelopeAward"); //清空红包分配表
            }
            List list = new List();
            int a = 1000;
            int sum = 0;

            if (radioButton2.Checked)//随机将额度
            {


                int[] arr = new int[Winner];
                Random rand = new Random(DateTime.Now.Millisecond);
                for (int i = 0; i < Winner - 1; i++)//人数减1
                {
                    arr[i] = rand.Next(Money);
                }

                Array.Sort(arr);
                // split数组中存放的就是最后分成10份的数,
                int[] split = new int[Winner];//人数 
                for (int i = 0; i < Winner; i++)//人数 
                {
                    if (i == 0)
                    {
                        split[i] = arr[i] - 0;
                    }
                    else if (i == Winner - 1)//人数减1
                    {
                        split[i] = Money - arr[i - 1];//钱数
                    }
                    else
                    {
                        split[i] = arr[i] - arr[i - 1];
                    }
                }
                // 这里可以验证和是不是1000
                for (int i = 0; i < Winner; i++)//人数 
                {
                    if (i < Winner - 1)//人数减1
                    {
                        Console.Write("{0}+", split[i]);
                    }
                    else
                    {
                        Console.Write("{0}=", split[i]);
                    }
                    sum += split[i];
                    list.Add(string.Format("INSERT INTO Data_RedEnvelopeAward VALUES ({0}, 0)", Convert.ToDouble(split[i] * 0.01)));
                }
                Console.WriteLine(sum);
            }
            if (radioButton1.Checked)//固定奖额度
            {
                int mw = (Money) / Winner;

                for (int i = 0; i < Winner; i++)//人数 
                {
                    Console.Write("{0}+", mw);
                    list.Add(string.Format("INSERT INTO Data_RedEnvelopeAward VALUES ({0}, 0)", Convert.ToDouble(mw * 0.01)));
                    sum += mw;
                }
                Console.WriteLine("=" + sum);
            }
            DbHelperSQL.ExecuteSqlTran(list);

            MessageBox.Show("生成完成");
        }

 

你可能感兴趣的:(随机数分配,中奖概率)