How to generate a cryptographically secure Double between 0 and 1?

Posted by Portman on Stack Overflow See other posts from Stack Overflow or by Portman
Published on 2010-05-18T03:45:39Z Indexed on 2010/05/18 3:50 UTC
Read the original article Hit count: 295

Filed under:
|

I know how to generate a random number between 0 and 1 using the NextDouble method of the pseudo-random number generator.

var rng1 = new System.Random();
var random1 = rng1.NextDouble(); // generates a random double between 0 and 1.0

And I know how to fill a random byte array using the cryptographically secure random number generator.

Byte[] bytes = new Byte[8];
var rng2 = new System.Security.Cryptography.RNGCryptoServiceProvider();
rng2.GetBytes(bytes); // generates 8 random bytes

But how can I convert the byte-array output of RNGCryptoServiceProvider into a random number between 0 (inclusive) and 1 (exclusive)?

© Stack Overflow or respective owner

Related posts about c#

Related posts about random