随机字符串

#region 随机字符串
/// <summary>
/// 生成随机字符串
/// </summary>
/// <param name="Length">指定长度</param>
/// <param name="Repeat">是否允许重复</param>
/// <param name="Seed">种子值[只需要第0个值,null表示使用默认种子值]</param>
/// <param name="All">字符集</param>
/// <returns></returns>
public static string Random( int Length, bool Repeat, int[] Seed, params char[] All )
{
if( All == null || All.Length == 0 )
{
return null;
}

Random rnd = new Random();
if( Seed != null && Seed.Length > 0 )
{
rnd = new Random( Seed[0] );
}

char[] aryAll = All;
if( !Repeat )
{
// 调整长度
Length = Math.Min( Length, All.Length );
// 复制字符集
aryAll = new char[All.Length];
All.CopyTo( aryAll, 0 );
}

string strReturn = string.Empty;
for( int i = 0; i < Length; i++ )
{
int[] arySeed = new int[1];
arySeed[0] = rnd.Next();
char ch = Char.Random( arySeed, aryAll );
strReturn += ch;

#region 不允许重复
if( !Repeat )
{
for( int j = 0; j < aryAll.Length; j++ )
{
if( aryAll[j] == ch )
{
aryAll[j] = aryAll[aryAll.Length-1];
break;
}
}

char[] aryTemp = new char[aryAll.Length-1];
for( int j = 0; j < aryAll.Length-1; j++ )
{
aryTemp[j] = aryAll[j];
}
aryAll = aryTemp;
}
#endregion
}

return strReturn;
}

/// <summary>
/// 生成随机字符串
/// </summary>
/// <param name="Length">指定长度</param>
/// <param name="Repeat">是否允许重复</param>
/// <param name="Seed">种子值[只需要第0个值,null表示使用默认种子值]</param>
/// <param name="All">字符集</param>
/// <returns></returns>
public static string Random( int Length, bool Repeat, int[] Seed, string All )
{
return Random( Length, Repeat, Seed, All.ToCharArray() );
}

/// <summary>
/// 返回指定数量,指定长度的随机字符串
/// </summary>
/// <param name="Count">指定的数量</param>
/// <param name="Length">指定的长度</param>
/// <param name="Repeat">是否允许重复</param>
/// <param name="Seed">种子值[只需要第0个值,null表示使用默认种子值]</param>
/// <param name="All">字符集</param>
/// <returns></returns>
public static string[] Random( uint Count, int Length, bool Repeat, int[] Seed, params char[] All )
{
if( Count == 0 )
{
return null;
}

Random rnd = new Random();
if( Seed != null && Seed.Length > 0 )
{
rnd = new Random( Seed[0] );
}

string[] aryReturn = new string[Count];
for( int i = 0; i < Count; i++ )
{
int[] arySeed = new int[1];
arySeed[0] = rnd.Next();
aryReturn[i] = Random( Length, Repeat, arySeed, All );
}

return aryReturn;
}

/// <summary>
/// 返回指定数量,指定长度的随机字符串
/// </summary>
/// <param name="Count">指定的数量</param>
/// <param name="Length">指定的长度</param>
/// <param name="Repeat">是否允许重复</param>
/// <param name="Seed">种子值[只需要第0个值,null表示使用默认种子值]</param>
/// <param name="All">字符集</param>
/// <returns></returns>
public static string[] Random( uint Count, int Length, bool Repeat, int[] Seed, string All )
{
return Random( Count, Length, Repeat, Seed, All.ToCharArray() );
}
#endregion

/// <summary>
/// 获取随机字符
/// </summary>
/// <param name="Seed">种子值[只需要第0个值,null表示使用默认种子值]</param>
/// <param name="All">字符集</param>
/// <returns></returns>
public static char Random( int[] Seed, params char[] All )
{
if( All == null || All.Length == 0 )
{
return '/0';
}

Random rnd;
if( Seed == null || Seed.Length == 0 )
{
rnd = new Random();
}
else
{
rnd = new Random( Seed[0] );
}

return All[rnd.Next( All.Length )];
}

/// <summary>
/// 获取随机字符
/// </summary>
/// <param name="Seed">种子值[只需要第0个值,null表示使用默认种子值]</param>
/// <param name="All">字符集</param>
/// <returns></returns>
public static char Random( int[] Seed, string All )
{
return Random( Seed, All.ToCharArray() );
}

• 0
点赞
• 0
评论
• 0
收藏
• 打赏
• 扫一扫，分享海报

05-03 1万+
07-22 2万+

05-22 188
03-01 228
02-24 175
04-04 101

Apq001

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。