当前位置: 编程技术>.net/c#/asp.net
c#生成随机数示例分享
来源: 互联网 发布时间:2014-10-28
本文导语: 代码如下:/// 构造随机数 种子static int GetRandomSeed(){ byte[] bytes = new byte[4]; System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider(); rng.GetBytes(by...
代码如下:
/// 构造随机数 种子
static int GetRandomSeed()
{
byte[] bytes = new byte[4];
System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
rng.GetBytes(bytes);
return BitConverter.ToInt32(bytes, 0);
}
/// 生成随机 数
static int rnd()
{
Random ran = new Random(GetRandomSeed());
int cnt = ran.Next(0,59);
return cnt;
}