随机字符串


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

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

©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

Apq001

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值