惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
SecWiki News
SecWiki News
酷 壳 – CoolShell
酷 壳 – CoolShell
爱范儿
爱范儿
量子位
M
MIT News - Artificial intelligence
GbyAI
GbyAI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
TaoSecurity Blog
TaoSecurity Blog
博客园 - 【当耐特】
H
Heimdal Security Blog
腾讯CDC
The Last Watchdog
The Last Watchdog
Security Archives - TechRepublic
Security Archives - TechRepublic
Hacker News: Ask HN
Hacker News: Ask HN
S
Schneier on Security
Microsoft Security Blog
Microsoft Security Blog
WordPress大学
WordPress大学
博客园 - 司徒正美
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
Cybersecurity and Infrastructure Security Agency CISA
S
SegmentFault 最新的问题
大猫的无限游戏
大猫的无限游戏
Application and Cybersecurity Blog
Application and Cybersecurity Blog
F
Full Disclosure
有赞技术团队
有赞技术团队
T
Tailwind CSS Blog
Engineering at Meta
Engineering at Meta
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Threatpost
月光博客
月光博客
A
Arctic Wolf
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
雷峰网
雷峰网
T
Troy Hunt's Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The Cloudflare Blog
D
DataBreaches.Net
O
OpenAI News
L
LINUX DO - 最新话题
宝玉的分享
宝玉的分享
小众软件
小众软件
V
Vulnerabilities – Threatpost
A
About on SuperTechFans
人人都是产品经理
人人都是产品经理
T
The Exploit Database - CXSecurity.com
Martin Fowler
Martin Fowler
美团技术团队
P
Privacy International News Feed

博客园 - phcis

c# 全角(SBC)和半角(DBC)相互转换函数 用HttpWebRequest做POST请求时返回Http 417 错误解决方法 使用webbroswer的一点技巧记录 计算字符串相似度及寻找最相似字符串的代码 asp.net中js返回false时阻止form提交的方法 【转】DIV+CSS 让文字居中于背景图 sql 语句实现分页 【转】datagridview的checkbox列,当修改checkbox状态时实时获得其准确状态值 c# 读取excel的一系列问题 - phcis - 博客园 由于使用“优易U盘加密软件”导致电脑无法关机/蓝屏等解决方法 批量删除数据库中所有表的记录(清空数据库) C# HttpRequest基础连接已经关闭: 接收时发生意外错误 GridView 动态绑定数据,包括2个或者多个值 - phcis - 博客园 近期动向 使用ajax导致滚动条复位的解决方法 AT编程常见问题与错误代码的意义 c# 发送email,正文支持html格式,包含附件 使用AutocompleteExtender无效或者没反应的原因记录 使用 DateTimePicker 控件显示和选择时间
【转】C#产生随机字符的两段代码
phcis · 2011-01-12 · via 博客园 - phcis

废话不多说,直接上代码。

代码1.

        Random m_rnd = new Random();
        
public char getRandomChar()
        {
            
int ret = m_rnd.Next(122);
            
while (ret < 48 || (ret > 57 && ret < 65|| (ret > 90 && ret < 97))
            {
                ret 
= m_rnd.Next(122);
            }
            
return (char)ret;
        }
        
public string getRandomString(int length)
        {
            StringBuilder sb 
= new StringBuilder(length);
            
for (int i = 0; i < length; i++)
            {
                sb.Append(getRandomChar());
            }
            
return sb.ToString();
        }     

代码2:--支持自定义字符混合

  //随机字符串生成器的主要功能如下: 
        
//1、支持自定义字符串长度
        
//2、支持自定义是否包含数字
        
//3、支持自定义是否包含小写字母
        
//4、支持自定义是否包含大写字母
        
//5、支持自定义是否包含特殊符号
        
//6、支持自定义字符集

        
///<summary>
        
///生成随机字符串
        
///</summary>
        
///<param name="length">目标字符串的长度</param>
        
///<param name="useNum">是否包含数字,1=包含,默认为包含</param>
        
///<param name="useLow">是否包含小写字母,1=包含,默认为包含</param>
        
///<param name="useUpp">是否包含大写字母,1=包含,默认为包含</param>
        
///<param name="useSpe">是否包含特殊字符,1=包含,默认为不包含</param>
        
///<param name="custom">要包含的自定义字符,直接输入要包含的字符列表</param>
        
///<returns>指定长度的随机字符串</returns>
        public string GetRnd(int length, bool useNum, bool useLow, bool useUpp, bool useSpe, string custom)
        {
            
byte[] b = new byte[4];
            
new System.Security.Cryptography.RNGCryptoServiceProvider().GetBytes(b);
            Random r 
= new Random(BitConverter.ToInt32(b, 0));
            
string s = null, str = custom;
            
if (useNum == true) { str += "0123456789"; }
            
if (useLow == true) { str += "abcdefghijklmnopqrstuvwxyz"; }
            
if (useUpp == true) { str += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; }
            
if (useSpe == true) { str += "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; }
            for (int i = 0; i < length; i++)
            {
                s 
+= str.Substring(r.Next(0, str.Length - 1), 1);
            }
            
return s;
        }