C# 解決Random偽隨機同一時間點隨機值重複的問題
阿新 • • 發佈:2020-06-12
方法1:
Random rand = new Random(Guid.NewGuid().GetHashCode()); int randomInt = rand.Next(1,10);
方法2:
byte[] bytes = new byte[4]; System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider(); rng.GetBytes(bytes); int t = BitConverter.ToInt32(bytes,0); Random rnd = new Random(t); int randomInt = rand.Next(1,10)