c#.net利用RNGCryptoServiceProvider产生任意范围强随机数的办法
int max = 100; //这样产生0 ~ 100的强随机数(含100)
int rnd = int.MinValue;
decimal _base = (decimal)long.MaxValue;
byte[] rndSeries = new byte[8];
System.Security.Cryptography.RNGCryptoServiceProvider rng
= new System.Security.Cryptography.RNGCryptoServiceProvider();
rng.GetBytes(rndSeries);
rnd = (int)(Math.Abs(BitConverter.ToInt64(rndSeries, 0)) / _base * (max+1));
//不含100需去掉+1
本文介绍了一种使用C#.NET中的RNGCryptoServiceProvider来生成指定范围内强随机数的方法。通过示例代码展示了如何创建0到100之间的随机整数,并提供了去除最大值的方法。
415

被折叠的 条评论
为什么被折叠?



