参考:http://www.cftea.com/c/2017/02/6808.asp
public static string GetRandomString(int length, bool useNum = false, bool useLow = false, bool useUpp = false, bool useSpe = false)
{
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] data = new byte[8];
string result = null, str = "";
if (useNum == true) { str += "0123456789"; }
if (useLow == true) { str += "abcdefghijklmnopqrstuvwxyz"; }
if (useUpp == true) { str += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; }
if (useSpe == true) { str += "!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"; }
System.Random r = new System.Random(BitConverter.ToInt32(data, 0));
for (int i = 0; i < length; i++)
{
rng.GetBytes(data);
result += str.Substring(r.Next(0, str.Length - 1), 1);
}
/*
for (int i = 0; i < length; i++)
{
rng.GetBytes(data);
int rnd = (int)Math.Round(Math.Abs(BitConverter.ToInt64(data, 0)) / (decimal)long.MaxValue * length, 0);
result += result.Substring(rnd, 1);
}
*/
return result;
}
最后
以上就是帅气眼睛最近收集整理的关于C# 产生真随机数(RNGCryptoServiceProvider)的全部内容,更多相关C#内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复