.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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s