﻿using System;
using System.Threading;

namespace Criteo.Profiling.Tracing.Utils
{
    /// <summary>
    /// Thread-safe random long generator.
    ///
    /// See "Correct way to use Random in multithread application"
    /// http://stackoverflow.com/questions/19270507/correct-way-to-use-random-in-multithread-application
    /// </summary>
    internal static class RandomUtils
    {
        private static int seed = Environment.TickCount;

        private static readonly ThreadLocal<Random> rand = new ThreadLocal<Random>(() => new Random(Interlocked.Increment(ref seed)));

        public static long NextLong()
        {
            return (long)((rand.Value.NextDouble() * 2.0 - 1.0) * long.MaxValue);
        }

    }
}

﻿using System;
using System.Threading;

namespace Criteo.Profiling.Tracing.Utils
{
    /// <summary>
    /// Thread-safe random long generator.
    ///
    /// See "Correct way to use Random in multithread application"
    /// http://stackoverflow.com/questions/19270507/correct-way-to-use-random-in-multithread-application
    /// </summary>
    internal static class RandomUtils
    {
        private static int _seed = Guid.NewGuid().GetHashCode();

        private static readonly ThreadLocal<Random> LocalRandom = new ThreadLocal<Random>(() => new Random(Interlocked.Increment(ref _seed)));

        public static long NextLong()
        {
            var buffer = new byte[8];
            LocalRandom.Value.NextBytes(buffer);
            return BitConverter.ToInt64(buffer, 0);
        }

    }
}

Change random to avoid identical seed
Change random to avoid identical seed

It seems it is possible for two processes started at the exact same time to get the same seed.
This could be the case for IIS where 2 handlers are started simultaneously.

Change-Id: I156fac5fe8c91c0931d38b0b6e5fe5cfb0e83665
