93. Cryptographic random numbers

This is much more complicated that you might imagine. (In fact, it's surprising that Microsoft didn't add methods to generate values within a range to its cryptographic random number generator so you wouldn't have to.) To understand why this is hard and to learn how to handle the difficulties, take the following discussion slowly. If you find that you’re lost, you may want to start over.

The .NET Framework's RNGCryptoServiceProvider class generates cryptographic random values. It is fairly easy to use but it has a catch—it only generates bytes. If you want some other kinds of data, such as double or int that lies between two bounds, then you need to convert the bytes accordingly.

To make the discussion easier, ...

Get The Modern C# Challenge now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.