Created
July 5, 2011 03:23
-
-
Save txdv/1064211 to your computer and use it in GitHub Desktop.
RandomGenerator for C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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