首页 > 代码库 > 控制随机数出现的概率
控制随机数出现的概率
//根据概率随机产生(1,2,3,4) 1,2,3,4的概率分别是0.3105,0.2564,0.3856,0.0475
public static int Rand2()
{
Random r = new Random(GetRandomSeed());
int num = r.Next(0, 10000);
int sth = 0;
if (num >= 0 && num < 3105)
{
sth = 1;
}
else if (num >= 3105 && num < 5669)
{
sth = 2;
}
else if (num >= 5669 && num < 9525)
{
sth = 3;
}
else if (num >= 9525 && num < 10000)
{
sth = 4;
}
return sth;
}
//解决random重复问题
public static int GetRandomSeed()
{
byte[] bytes = new byte[4];
System.Security.Cryptography.RNGCryptoServiceProvider mg = new System.Security.Cryptography.RNGCryptoServiceProvider();
mg.GetBytes(bytes);
return BitConverter.ToInt32(bytes, 0);
}