因为普通的Random随机数是通过时间来生成的,如果在短时间调用多次的话会造成生成的随机数一致。
方法1:
每生成完随机数后让线程睡眠1ms:
thread.sleep(1);
方法2:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12/// <summary> /// 描 述:创建加密随机数生成器 生成强随机种子 /// </summary> /// <returns></returns> static int GetRandomSeed() { byte[] bytes = new byte[4]; System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider(); rng.GetBytes(bytes); return BitConverter.ToInt32(bytes, 0); } string num = new random(GetRandomSeed()).Next(10);
最后
以上就是醉熏跳跳糖最近收集整理的关于C#生成真随机数的全部内容,更多相关C#生成真随机数内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复