C#生成不重复随机字符串类

所属分类: 软件编程 / C#教程 阅读数: 55
收藏 0 赞 0 分享

本文实例讲述了C#生成不重复随机字符串类。分享给大家供大家参考。具体如下:

这个C#类用于随机产生不重复的字符串,可以指定字符串范围,可以指定要产生字符串的长度

using System;
namespace DotNet.Utilities
{
  public class RandomOperate
  {
    // 一:随机生成不重复数字字符串 
    private int rep = 0;
    public string GenerateCheckCodeNum(int codeCount)
    {
      string str = string.Empty;
      long num2 = DateTime.Now.Ticks + this.rep;
      this.rep++;
      Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> this.rep)));
      for (int i = 0; i < codeCount; i++)
      {
        int num = random.Next();
        str = str + ((char)(0x30 + ((ushort)(num % 10)))).ToString();
      }
      return str;
    }
    //方法二:随机生成字符串(数字和字母混和)
    public string GenerateCheckCode(int codeCount)
    {
      string str = string.Empty;
      long num2 = DateTime.Now.Ticks + this.rep;
      this.rep++;
      Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> this.rep)));
      for (int i = 0; i < codeCount; i++)
      {
        char ch;
        int num = random.Next();
        if ((num % 2) == 0)
        {
          ch = (char)(0x30 + ((ushort)(num % 10)));
        }
        else
        {
          ch = (char)(0x41 + ((ushort)(num % 0x1a)));
        }
        str = str + ch.ToString();
      }
      return str;
    }
    #region
    /// <summary>
    /// 从字符串里随机得到,规定个数的字符串.
    /// </summary>
    /// <param name="allChar"></param>
    /// <param name="CodeCount"></param>
    /// <returns></returns>
    private string GetRandomCode(string allChar, int CodeCount)
    {
      //string allChar = "1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,i,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
      string[] allCharArray = allChar.Split(',');
      string RandomCode = "";
      int temp = -1;
      Random rand = new Random();
      for (int i = 0; i < CodeCount; i++)
      {
        if (temp != -1)
        {
          rand = new Random(temp * i * ((int)DateTime.Now.Ticks));
        }
        int t = rand.Next(allCharArray.Length - 1);
        while (temp == t)
        {
          t = rand.Next(allCharArray.Length - 1);
        }
        temp = t;
        RandomCode += allCharArray[t];
      }
      return RandomCode;
    }
    #endregion
  }
}

希望本文所述对大家的C#程序设计有所帮助。

更多精彩内容其他人还在看

C#使用oledb读取excel表格内容到datatable的方法

这篇文章主要介绍了C#使用oledb读取excel表格内容到datatable的方法,涉及C#操作oledb及datatable的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#使用oledb操作excel文件的方法

这篇文章主要介绍了C#使用oledb操作excel文件的方法,涉及C#中oledb操作excel的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#使用IHttpModule接口修改http输出的方法

这篇文章主要介绍了C#使用IHttpModule接口修改http输出的方法,涉及C#操作IHttpModule接口的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#给图片加水印的简单实现方法

这篇文章主要介绍了C#给图片加水印的简单实现方法,涉及C#操作图片的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#生成随机数的方法小结

这篇文章主要介绍了C#生成随机数的方法,实例总结了C#生成随机数的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#使用jQuery实现无刷新评论提交的方法

这篇文章主要介绍了C#使用jQuery实现无刷新评论提交的方法,涉及C#结合jQuery进行Ajax操作的相关技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#读取中文文件出现乱码的解决方法

这篇文章主要介绍了C#读取中文文件出现乱码的解决方法,涉及C#中文编码的操作技巧,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C#图像对比度调整的方法

这篇文章主要介绍了C#图像对比度调整的方法,涉及C#实现图像对比度操作的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#图像灰度级拉伸的方法

这篇文章主要介绍了C#图像灰度级拉伸的方法,涉及C#灰度操作的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#图像线性变换的方法

这篇文章主要介绍了C#图像线性变换的方法,涉及C#操作图像线性变换的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多