Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request statsd#133 from otac0n/patch-1
Improved the quality of randomness used for sampling.
  • Loading branch information
mrtazz committed Jul 31, 2012
2 parents a98c905 + da8d73b commit 4682f7f
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions examples/csharp_example.cs
Expand Up @@ -8,7 +8,17 @@ namespace Statsd
public class StatsdPipe : IDisposable
{
private readonly UdpClient udpClient;
private readonly Random random = new Random();

[ThreadStatic]
private static Random random;

private static Random Random
{
get
{
return random ?? (random = new Random());
}
}

public StatsdPipe(string host, int port)
{
Expand Down Expand Up @@ -101,7 +111,7 @@ protected bool Send(double sampleRate, params string[] stats)
{
foreach (var stat in stats)
{
if (random.NextDouble() <= sampleRate)
if (Random.NextDouble() <= sampleRate)
{
var statFormatted = String.Format("{0}|@{1:f}", stat, sampleRate);
if (DoSend(statFormatted))
Expand Down

0 comments on commit 4682f7f

Please sign in to comment.