.NET Random Numbers

.NET gives you two ways to generate random numbers – a fast but insecure way, and a cryptographically pseudorandom way which is safe to use for cryptography. Both are easy to use.

For non-secure purposes, create a single instance of Random – this can be a singleton or a static field – just don’t create a new one every time you need it, because you might end up with dupes that way. Random is also not thread-safe, so make sure you access it in a thread-safe way if there’s the possibility of multiple threads accessing it.

View code on GitHub

Leave a Reply

Your email address will not be published.