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

推荐订阅源

J
Java Code Geeks
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
V2EX
小众软件
小众软件
WordPress大学
WordPress大学
Apple Machine Learning Research
Apple Machine Learning Research
Recent Announcements
Recent Announcements
有赞技术团队
有赞技术团队
MongoDB | Blog
MongoDB | Blog
C
Check Point Blog
S
Schneier on Security
C
Cybersecurity and Infrastructure Security Agency CISA
The Cloudflare Blog
V
Vulnerabilities – Threatpost
The Hacker News
The Hacker News
T
Threatpost
T
Tenable Blog
aimingoo的专栏
aimingoo的专栏
IT之家
IT之家
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
C
CERT Recently Published Vulnerability Notes
U
Unit 42
Spread Privacy
Spread Privacy
博客园 - 司徒正美
Hacker News: Ask HN
Hacker News: Ask HN
C
CXSECURITY Database RSS Feed - CXSecurity.com
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
阮一峰的网络日志
阮一峰的网络日志
SecWiki News
SecWiki News
云风的 BLOG
云风的 BLOG
The Register - Security
The Register - Security
AWS News Blog
AWS News Blog
月光博客
月光博客
Security Latest
Security Latest
H
Heimdal Security Blog
S
Secure Thoughts
博客园 - 聂微东
PCI Perspectives
PCI Perspectives
博客园 - 叶小钗
Scott Helme
Scott Helme
O
OpenAI News
Google DeepMind News
Google DeepMind News
Google DeepMind News
Google DeepMind News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Security @ Cisco Blogs
NISL@THU
NISL@THU
S
Securelist
Latest news
Latest news
P
Proofpoint News Feed
博客园 - 【当耐特】

博客园 - MaxIE

jQuery选择器和选取方法 为什么Android的图片质量会比iPhone的差? 百度地图计算两坐标点之间距离计算 .net下BerkeleyDB操作封装C#版(附单元测试) MS SQL SERVER索引优化相关查询 SSD在SQLServer中的应用 Speech两种使用方法 让.net程序自动运行在管理员权限下 “请求的操作无法在使用用户映射区域打开的文件上执行”问题处理 json.net处理复杂json 未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序 解决办法 excel中根据单元格背景颜色进行数据筛选(excel2003实现方法) 跨平台加密版 SQLite 3 - wxSQLite3 数据库sql2000错误8908及处理 jQuery2011年年度最佳插件 jQ中文API离线版下载(适用版本1.4.4,1.5,1.5.1,1.5.2,1.6,1.6.1,1.6.2) 方便的CSS和jQuery下拉菜单解决方案 sql2000无法执行查询及未找到提供程序解决办法 SQL Server优化SELECT语句方法
C#随机字符串随机性不足的解決方式(随机数重复)
MaxIE · 2013-02-18 · via 博客园 - MaxIE

这个其实不是问题,一般情况下倒是不会出问题的,代码如下:

private string random_str(int length)
{

int number;
string checkCode = String.Empty;
int iSeed = 10;
Random ro = new Random(10);
long tick = DateTime.Now.Ticks;
Random random = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));
for (int i = 0; i < length; i++)
{
number = random.Next();
number = number % 36;
if (number < 10)
{
number += 48;
}
else
{
number += 55;
}
checkCode += ((char)number).ToString();
}
return checkCode;
}

但是如果你蛋疼一下出现这种情况就不行了

random_str(4)+random_str(4)+random_str(4)

你会发新生成的12个字符其实是3个相同的字符串,于是心里面无数个草泥马奔腾而过。

其实稍微加个sleep就能解决这个问题:

private string random_str(int length)
{
System.Threading.Thread.Sleep(1);
int number;
string checkCode = String.Empty;
int iSeed = 10;
Random ro = new Random(10);
long tick = DateTime.Now.Ticks;
Random random = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));
for (int i = 0; i < length; i++)
{
number = random.Next();
number = number % 36;
if (number < 10)
{
number += 48;
}
else
{
number += 55;
}
checkCode += ((char)number).ToString();
}
return checkCode;
}

本质上更接近真实的随机