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

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - 吴尔平

gtest 的彩色信息输出 + boost.test 的内存泄漏检测及定位 在低版本的 vc 中使用 vc 10.0 的新特性 使用另一个blog: http://blog.csdn.net/WuErPing scons + swig 如何在 vista 使用 Device Emulator 连接internet vc9 Feature Pack Beta tr1 的一些问题 NSIS Kill Process (转贴) C#与一个彩票页面 - 吴尔平 - 博客园 py2exe 转换 pytetris - 吴尔平 关于模板化的friend class python 读取 windows event log 的简短代码 IronPython 1.0 Release Candidate (转贴 ( 据说比c实现的快1.5!) ) Visual Studio Service Pack (转贴) The 16th Annual Jolt Product Excellence Award Winners (转贴) C++/CLI FAQ (逐步整理中) C++/CLI singleton模式 (双重检测锁实现) 如何在 VS2005 的 Team Unit Testing frameworks 中测试 Native Code (C++ ) 2005 CRT memory leaks 改变 SQL Server 2000 所有对象的所有者
C# 的 random shuffle
吴尔平 · 2006-12-31 · via 博客园 - 吴尔平

      2006马上就要过去了,blog也长草了。贴上一点代码,意思一下

      前段时间在C#里需要用到random shuffle功能,一时没找到合适的代码,就按自己的理解写了一段,所得到结果也能满足自己的需要。值得注意的一点是随机数生成器的选择。直接以Random做为随机数生成器因为时钟精度问题,在一个小的时间段内会得到同样的伪随机数序列,你shuffle后会得到同一个结果。.net提供了RNGCryptoServiceProvider可以避免这种情况,下面是几种用法的示例

 1     /// <summary>
 2     /// RandomShuffle
 3     /// WuErPing 2006/12/07
 4     /// </summary>
 5     public sealed class RandomShuffle
 6     {
 7         private RandomShuffle() { }
 8 
 9         // pseudo-random number generator, using a time-dependent default seed value. 
10         static public List<int> Shuffle(int size)
11         {
12             List<int> list = new List<int>(size);
13             for (int i = 0; i < size; ++i)
14             {
15                 list.Insert(i, i);
16             }
17             System.Random random = new Random();
18             for (int i = 0; i < list.Count; ++i)
19             {
20                 int var = random.Next(0, list.Count);
21                 int temp = list[i];
22                 list[i] = list[var];
23                 list[var] = temp;
24             }
25 
26             return list;
27         }
28 
29         // using a RNGCryptoServiceProvider().GetHashCode() seed value 
30         static public List<int> ShuffleEx(int size)
31         {
32             List<int> list = new List<int>(size);
33             for (int i = 0; i < size; ++i)
34             {
35                 list.Insert(i, i);
36             }
37             System.Random random = new Random(new RNGCryptoServiceProvider().GetHashCode());
38             for (int i = 0; i < list.Count; ++i)
39             {
40                 int var = random.Next(0, list.Count);
41                 int temp = list[i];
42                 list[i] = list[var];
43                 list[var] = temp;
44             }
45 
46             return list;
47         }
48 
49         // Cryptographic random number generators create cryptographically strong random values
50         static public List<int> ShufflePro(int size)
51         {
52             List<int> list = new List<int>(size);
53             for (int i = 0; i < size; ++i)
54             {
55                 list.Insert(i, i);
56             }
57             byte[] randomBytes = new Byte[4];
58             RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
59             for (int i = 0; i < list.Count; ++i)
60             {
61                 rng.GetNonZeroBytes(randomBytes);
62                 int randomSeed = (randomBytes[0<< 24| (randomBytes[1<< 16| (randomBytes[2<< 8| randomBytes[3];
63                 int var = randomSeed % list.Count;
64                 //var = System.Math.Abs(var);
65                 if (var < 0) var *= -1;
66                 int temp = list[i];
67                 list[i] = list[var];
68                 list[var] = temp;
69             }
70             return list;
71         }
72     }

注:如果要深究random shuffle算法,可以看标准C++的random_shuffle的实现。根据SGI的文档http://www.sgi.com/tech/stl/random_shuffle.html,算法来自
[1] This algorithm is described in section 3.4.2 of Knuth (D. E. Knuth, The Art of Computer Programming. Volume 2: Seminumerical Algorithms, second edition. Addison-Wesley, 1981). Knuth credits Moses and Oakford (1963) and Durstenfeld (1964).

附:vc8的random_shuffle的实现

 1         // TEMPLATE FUNCTION random_shuffle
 2 template<class _RanIt,
 3     class _Diff> inline
 4     void _Random_shuffle(_RanIt _First, _RanIt _Last, _Diff *)
 5     {    // shuffle [_First, _Last)
 6     _DEBUG_RANGE(_First, _Last);
 7     const int _RANDOM_BITS = 15;    // minimum random bits from rand()
 8     const int _RANDOM_MAX = (1U << _RANDOM_BITS) - 1;
 9 
10     _RanIt _Next = _First;
11     for (unsigned long _Index = 2++_Next != _Last; ++_Index)
12         {    // assume unsigned long big enough for _Diff count
13         unsigned long _Rm = _RANDOM_MAX;
14         unsigned long _Rn = ::rand() & _RANDOM_MAX;
15         for (; _Rm < _Index && _Rm != ~0UL;
16             _Rm = _Rm << _RANDOM_BITS | _RANDOM_MAX)
17             _Rn = _Rn << _RANDOM_BITS
18                 | (::rand() & _RANDOM_MAX);    // build random value
19 
20         std::iter_swap(_Next, _First + _Diff(_Rn % _Index));    // swap a pair
21         }
22     }
23