一. 生成隨機(jī)數(shù)(可設(shè)定范圍)
使用C#自帶類System.Radom(int seed),來生成隨機(jī)數(shù),
使用Guid.NewGuid().GetHashCode(),生成新的Guid,然后獲取它的Hash Code(int),作為種子放入System.Radom(int seed)使用。
種子作用是使每次生成的數(shù)字分布更加隨機(jī)。
所有的隨機(jī)數(shù)生成算法都是偽隨機(jī)數(shù)。
二. 使用for循環(huán),循環(huán)生成隨機(jī)數(shù),然后劃分范圍(概率分配),對落入不同范圍內(nèi)的隨機(jī)數(shù)進(jìn)行計數(shù),最后計算出實際概率值。
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;
//添加整形變量為循環(huán)次數(shù)與概率分母,更改循環(huán)次數(shù)測試時,只要修改一個變量,即可得出正確概率
static int Cnum = 10000;
static void Main(string[] args)
{ //循環(huán)次數(shù)更換為變量
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);//抽獎概率隨機(jī)數(shù)
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;
}
}
}
//隱式轉(zhuǎn)換時,要先把Cnum轉(zhuǎn)換成double類型,然后除法的結(jié)果才能隱式轉(zhuǎn)換為double
double cnum = Convert.ToDouble(Cnum);
Console.Write("1等獎: " + (a / cnum).ToString("0.####%") +" 次數(shù)"+a+ "\n");
Console.Write("2等獎: " + (b / cnum).ToString("0.####%") + " 次數(shù)" + b + "\n");
Console.Write("3等獎: " + (c / cnum).ToString("0.####%") + " 次數(shù)" + c + "\n");
Console.Write("4等獎: " + (d / cnum).ToString("0.####%") + " 次數(shù)" + d + "\n");
Console.Write("5等獎: " + (e / cnum).ToString("0.####%") + " 次數(shù)" + e + "\n");
Console.Write("6等獎: " + (f / cnum).ToString("0.####%") + " 次數(shù)" + f + "\n");
Console.Write("7等獎: " + (g / cnum).ToString("0.####%") + " 次數(shù)" + g + "\n");
Console.Write("7等獎: " + (ii / cnum).ToString("0.####%") + " 次數(shù)" + ii + "\n");
Console.Write("7等獎: " + (h / cnum).ToString("0.####%") + " 次數(shù)" + h + "\n");
Console.Write("7等獎: " + (j / cnum).ToString("0.####%") + " 次數(shù)" + j + "\n");
Console.ReadLine();
}
}
}