经常用到随机数,但是总忘记怎么写,总是得去百度查看一下,补充一下笔记,记录一下怎么生成随机数,方便以后查找
///
/// 生成8位随机数【仅包含数字】
///
/// 随机数长度
///
public static string Get_Random_Int(int len)
{
string s = "1234567890";
string reValue = string.Empty;
Random rnd = new Random(GetNewSeed());
while (reValue.Length < len)
{
string s1 = s[rnd.Next(0, s.Length)].ToString();
if (reValue.IndexOf(s1) == -1) reValue += s1;
}
return reValue;
}
///
/// 生成随机数的种子
///
///
private static int GetNewSeed()
{
byte[] rndBytes = new byte[4];
System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
rng.GetBytes(rndBytes);
return BitConverter.ToInt32(rndBytes, 0);
}
记录一下,毕竟这个常用,但是就是记不住
经常用到随机数,但是总忘记怎么写,总是得去百度查看一下,补充一下笔记,记录一下怎么生成随机数,方便以后查找
///
/// 生成8位随机数【仅包含数字】
///
/// 随机数长度
///
public static string Get_Random_Int(int len)
{
string s = "1234567890";
string reValue = string.Empty;
Random rnd = new Random(GetNewSeed());
while (reValue.Length < len)
{
string s1 = s[rnd.Next(0, s.Length)].ToString();
if (reValue.IndexOf(s1) == -1) reValue += s1;
}
return reValue;
}
///
/// 生成随机数的种子
///
///
private static int GetNewSeed()
{
byte[] rndBytes = new byte[4];
System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
rng.GetBytes(rndBytes);
return BitConverter.ToInt32(rndBytes, 0);
}
记录一下,毕竟这个常用,但是就是记不住
每一个童年的梦想都值得用青春去捍卫!