47. 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 Improving your C# Skills now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.