You need to generate some random numbers in your application and started to use System.Random, but a colleague suggests using RNGCryptoServiceProvider instead. Do you retool your code to use RNGCryptoServiceProvider, or is there a better way to proceed?

What you need is an adapter that takes the functionality of one class and "adapts" it to fit the interface provided (or expected) by another. In this case, you want to adapt the functionality of RNGCryptoServiceProvider to the interface of Random.

In the September issue of MSDN Magazine, our gone-but-not-forgotten former technical editor Stephen Toub and Shawn Farkas demonstrate this adapter pattern in a CryptoRandom class that derives from Random.

Don't forget to browse the code online for their solution.

For more of Stephen's writing on .NET development, check out the .NET Matters archive on the MSDN Magazine web site.

Happy coding!

Terrence Dorsey