随机字符串


  #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

其中Char.Random定义如下:


  /// <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() );
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值