Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get random double value out of random byte array values?

Tags:

c#

.net

random

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?

like image 752
Kamil Zadora Avatar asked Dec 31 '08 17:12

Kamil Zadora


1 Answers

byte[] result = new byte[8];
rng.GetBytes(result);
return (double)BitConverter.ToUInt64(result,0) / ulong.MaxValue;
like image 177
mmx Avatar answered Nov 14 '22 21:11

mmx