1. 程式人生 > WINDOWS開發 >C# 解決Random偽隨機同一時間點隨機值重複的問題

C# 解決Random偽隨機同一時間點隨機值重複的問題

方法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)