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)

  

  

posted @ 2020-06-12 09:05  拍空格  阅读(632)  评论(0编辑  收藏  举报