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

推荐订阅源

Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
SegmentFault 最新的问题
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Attack and Defense Labs
Attack and Defense Labs
F
Full Disclosure
Vercel News
Vercel News
N
News | PayPal Newsroom
The GitHub Blog
The GitHub Blog
H
Hacker News: Front Page
H
Heimdal Security Blog
P
Privacy International News Feed
博客园 - 司徒正美
Google DeepMind News
Google DeepMind News
N
Netflix TechBlog - Medium
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cisco Blogs
L
Lohrmann on Cybersecurity
D
Docker
Recent Announcements
Recent Announcements
Security Archives - TechRepublic
Security Archives - TechRepublic
人人都是产品经理
人人都是产品经理
C
CXSECURITY Database RSS Feed - CXSecurity.com
P
Proofpoint News Feed
T
Tailwind CSS Blog
C
Check Point Blog
博客园 - 叶小钗
Google Online Security Blog
Google Online Security Blog
Martin Fowler
Martin Fowler
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
S
Secure Thoughts
博客园 - Franky
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
P
Palo Alto Networks Blog
Latest news
Latest news
量子位
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)
The Cloudflare Blog
Last Week in AI
Last Week in AI
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Cyberwarzone
Cyberwarzone
小众软件
小众软件
Cisco Talos Blog
Cisco Talos Blog
Hacker News: Ask HN
Hacker News: Ask HN
T
Threatpost
T
Tenable Blog
P
Privacy & Cybersecurity Law Blog
WordPress大学
WordPress大学

博客园 - Zhongjian Zhang

.net framework profiles /.net framework 配置 人生的意义岂止是谋生-新视野大学英语第二册第八单元A课文 - Zhongjian Zhang - 博客园 格式化类型和分析字符串 类接口/Class Interface LG 3D投影 directory searchPattern to regex pattern Textbox/ComboBox 自动完成AutoCompletion Bloom's Taxonomy/布鲁姆分类 Data Structure/数据结构 in .Net Custom Shapes/Combin Shapes/合并图形 in PowerPoint 2010 scale up vs scale out/Scale vertically vs. horizontally Window Types(Tool windows/document windows)/窗口类型(工具窗口/文档窗口) in VS VS2010中shortcut key快捷键一览下载 Windows Azure Storage Explorer List System Error Codes/系统错误代码 图解500强 超级计算机 In graphics: Supercomputing superpowers FTP active mode and assive mode(ftp 主动模式/被动模式) Ftp commands and options 为VM制作可引导的操作系统ISO
随机long(Random long/NextLong)
Zhongjian Zhang · 2010-06-21 · via 博客园 - Zhongjian Zhang

 在.net FCL中random没有生成long类型的随机数,不过random可以生成0-1的随机数,这就可以帮助实现random Long.

如下为一简单实现(.net 3.5):

Random NextLong

 1     static class Common
 2     {
 3         public static long NextLong(this Random random,long minValue,long maxValue)
 4         {
 5             if (minValue > maxValue)
 6             {
 7                 throw new ArgumentException("minValue is great than maxValue""minValue");
 8             }
 9             long num = maxValue - minValue;
10             return minValue + (long) (random.NextDouble() * num);
11         }
12     }

 使用如下:

1 Random random = new Random();
2 random.NextLong(MinSize, MaxSize);

由于random.NextDouble() 是根据Int32来生成的,其个数为Int32.Max个。如果上述NextLong连续调用maxValue-minValue+1次,在maxValue-minValue+1 > Int32.Max时,未必能生成所有可能近视分布均匀的随机数据。

现 更新如下:

代码

public static long NextLong(this Random random, long min, long max)
{
byte[] minArr
= BitConverter.GetBytes(min);
int hMin
= BitConverter.ToInt32(minArr, 4);
int lMin
= BitConverter.ToInt32(new byte[] { minArr[0], minArr[1], minArr[2], minArr[3] }, 0);

byte[] maxArr

= BitConverter.GetBytes(max);
int hMax
= BitConverter.ToInt32(maxArr, 4);
int lMax
= BitConverter.ToInt32(new byte[] { maxArr[0], maxArr[1], maxArr[2], maxArr[3] }, 0);if (random == null)
{
random
= new Random();
}

int h

= random.Next(hMin, hMax);
int l
= 0;
if (h == hMin)
{
l
= random.Next(Math.Min(lMin, lMax), Math.Max(lMin, lMax));
}
else
{
l
= random.Next(0, Int32.MaxValue);
}

byte[] lArr

= BitConverter.GetBytes(l);
byte[] hArr
= BitConverter.GetBytes(h);
byte[] result
= new byte[8];for (int i = 0; i < lArr.Length; i++)
{
result[i]
= lArr[i];
result[i
+ 4] = hArr[i];
}
return BitConverter.ToInt64(result, 0);
}