diff --git a/src/StatsdClient/RandomGenerator.cs b/src/StatsdClient/RandomGenerator.cs
index edfd061..4a1d03f 100644
--- a/src/StatsdClient/RandomGenerator.cs
+++ b/src/StatsdClient/RandomGenerator.cs
@@ -4,10 +4,10 @@ namespace StatsdClient
{
public class RandomGenerator : IRandomGenerator
{
- readonly Random _random;
+ readonly ThreadSafeRandom _random;
public RandomGenerator()
{
- _random = new Random();
+ _random = new ThreadSafeRandom();
}
public bool ShouldSend(double sampleRate)
diff --git a/src/StatsdClient/StatsdClient.csproj b/src/StatsdClient/StatsdClient.csproj
index c985c1a..87f7138 100644
--- a/src/StatsdClient/StatsdClient.csproj
+++ b/src/StatsdClient/StatsdClient.csproj
@@ -51,6 +51,7 @@
+
@@ -66,4 +67,4 @@
-->
-
+
\ No newline at end of file
diff --git a/src/StatsdClient/ThreadSafeRandom.cs b/src/StatsdClient/ThreadSafeRandom.cs
new file mode 100644
index 0000000..91d15e2
--- /dev/null
+++ b/src/StatsdClient/ThreadSafeRandom.cs
@@ -0,0 +1,34 @@
+using System;
+
+namespace StatsdClient
+{
+ public class ThreadSafeRandom
+ {
+ private static readonly Random _global = new Random();
+
+ [ThreadStatic]
+ private static Random _local;
+
+ private Random Local
+ {
+ get
+ {
+ if (_local == null)
+ {
+ int seed;
+ lock (_global)
+ {
+ seed = _global.Next();
+ }
+ _local = new Random(seed);
+ }
+ return _local;
+ }
+ }
+
+ public double NextDouble()
+ {
+ return Local.NextDouble();
+ }
+ }
+}