Skip to content

Instantly share code, notes, and snippets.

@txdv
Created July 5, 2011 03:23
Show Gist options
  • Save txdv/1064211 to your computer and use it in GitHub Desktop.
Save txdv/1064211 to your computer and use it in GitHub Desktop.
RandomGenerator for C#
using System.Security.Cryptography;
namespace Awesome
{
public static class RandomGenerator
{
private static RNGCryptoServiceProvider global =
new RNGCryptoServiceProvider();
[ThreadStatic]
private static Random local;
public static int Next()
{
Random inst = local;
if (inst == null) {
byte[] buffer = new byte[4];
global.GetBytes(buffer);
local = inst = new Random(BitConverter.ToInt32(buffer, 0));
}
return inst.Next();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment