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

推荐订阅源

博客园_首页
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
G
Google Developers Blog
B
Blog
Engineering at Meta
Engineering at Meta
阮一峰的网络日志
阮一峰的网络日志
The Register - Security
The Register - Security
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 叶小钗
The Cloudflare Blog
The Hacker News
The Hacker News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
雷峰网
雷峰网
F
Fortinet All Blogs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Hackread – Cybersecurity News, Data Breaches, AI and More
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
A
About on SuperTechFans
量子位
Recorded Future
Recorded Future
博客园 - 三生石上(FineUI控件)
H
Help Net Security
Help Net Security
Help Net Security
P
Palo Alto Networks Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Troy Hunt's Blog
W
WeLiveSecurity
V
Vulnerabilities – Threatpost
T
The Exploit Database - CXSecurity.com
Know Your Adversary
Know Your Adversary
Apple Machine Learning Research
Apple Machine Learning Research
Scott Helme
Scott Helme
N
News | PayPal Newsroom
AWS News Blog
AWS News Blog
D
DataBreaches.Net
Blog — PlanetScale
Blog — PlanetScale
MongoDB | Blog
MongoDB | Blog
B
Blog RSS Feed
腾讯CDC
J
Java Code Geeks
Microsoft Azure Blog
Microsoft Azure Blog
TaoSecurity Blog
TaoSecurity Blog
GbyAI
GbyAI
Y
Y Combinator Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
D
Docker

博客园 - 赶路人之刚出发

集成WebSecurity的Authorize进行身份验证时,数据库连接报错问题 Html.ActionLink传递参数 Automapper结合EF实现insert,update方法 MVC中使用RemoteAttribute异步远程验证 Html.RenderPartial WebMatrix.WebSecurity创建自定义用户属性 强类型view中List<Model〉问题 ViewBag任意属性的实现方法 params关键字 配置LINQ中的datacontext的log路径,以记录datacontext执行了的查询sql SortedList LINQ join/left join/cross join/group by/group join/sortedlist/cast Linq to objects示例 yield return 和 Func Lamda表达式 IDisposable 匿名类型与扩展方法 对象初始化器和集合初始化器 C#自动属性
.net random伪随机数
赶路人之刚出发 · 2013-04-11 · via 博客园 - 赶路人之刚出发

.net中的Randome类可以用于获取随机数,但并非真正意义上的随机,缺点有二:

当种子相同时,生成的随机数列一定是相同的,默认用的是当前系统启动后的毫秒数为种子;

生成的随机数列很有可能有重复,也就是所有随机数出现的概率并不完全相同;

比如:

View Code

Random mr = new Random(10);
            for (int s = 0; s < 10; s++)
            {
                Console.WriteLine(mr.Next(10));
            }
            Console.WriteLine("-----");
            mr = new Random(10);
            for (int s = 0; s < 10; s++)
            {
                Console.WriteLine(mr.Next(10));
            }

执行结果为:

为避免以上两个问题,有如下几种方案:

1、
Random 类是根据CPU的时钟来产生随机数,如果间隔时间太短有时是会产生相同的随机数,
可以通过暂停下Thread.sleep(6); 
2、
可以通过设置一个List列表,判断,如果已经有这个数字,则重新生成;否则,添加到List中
private List<int> GetRndCode(int codeLength) 
        {
            List<int> rndLst = new List<int>();
            int temp = 0;
            int chr=0;
            Random rnd = new Random();
            while (temp < codeLength)
            {
                chr = rnd.Next(100);
                if (!rndLst.Contains(chr))
                {
                    temp++;
                    rndLst.Add(chr);
                }
            }
            return rndLst;
        }
有可能会耗时很长。是否会死循环。不确定。
3、如果有一定的时间间隔的话,也可以通过使用Random rd = new Random(DateTime.Now.Millisecond)来达到随机数的目的。
4、网络上有人提议用:Random rd = new Random(Guid.NewGuid().GetHashCode()) 说是可以生成不重复的随机数。<不能确定是否正确,但是NewGuid()是肯定不会重复的。>
总结:个人认为第二种方法是最好的。 计算机运行效率越来越高,同时运行的可能性很大。时间的操作不确定性很高。