如何获得随机double值了随机字节数组值吗? [英] How to get random double value out of random byte array values?
本文介绍了如何获得随机double值了随机字节数组值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用RNGCryptoServiceProvider作为我的随机数的来源。因为它只能输出它们作为字节值数组我怎样才能将它们转换为0到1,而结果preserving一致性?
双重价值解决方案
byte []的结果=新的字节[8]。
rng.GetBytes(结果);
返程(双)BitConverter.ToUInt64(结果,0)/ ulong.MaxValue;
I would like to use RNGCryptoServiceProvider as my source of random numbers. As it only can output them as an array of byte values how can I convert them to 0 to 1 double value while preserving uniformity of results?
解决方案
byte[] result = new byte[8];
rng.GetBytes(result);
return (double)BitConverter.ToUInt64(result,0) / ulong.MaxValue;
这篇关于如何获得随机double值了随机字节数组值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文