C#使用 RNGCryptoServiceProvider 生成隨機數
一個簡單的方法,但不夠可靠
static void Main(string[] args) { // code from DevCurry.com byte[] randomBytes = new byte[4]; Random rando = new Random(); rando.NextBytes(randomBytes); foreach (byte byteValue in randomBytes) Console.Write("{0, 4}", byteValue); Console.ReadLine(); }
可靠的方法
static void Main(string[] args) { // code from DevCurry.com byte[] randomBytes = new byte[4]; RNGCryptoServiceProvider rngCrypto = new RNGCryptoServiceProvider(); rngCrypto.GetBytes(randomBytes); Int32 rngNum = BitConverter.ToInt32(randomBytes, 0); Console.WriteLine(rngNum); Console.ReadLine(); }
本文由用戶 wufflina 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!