.net 隨機數

system.Random類生成的隨機數是僞隨機,所以使用system.Security.Cryptography.RNGCryptoServiceProvider的類

 

 1  static int GetRandomSeed()
 2     {
 3         byte[] bytes = new byte[4];
 4         System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
 5         rng.GetBytes(bytes);
 6         return BitConverter.ToInt32(bytes, 0);
 7     }
 8 
 9     static int GetNumber()
10     {
11         return Math.Abs(GetRandomSeed());
12     }
相關文章
相關標籤/搜索