diff options
author | Peter Marcu <Peter.Marcu@Microsoft.com> | 2016-10-13 14:55:50 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-13 14:55:50 -0700 |
commit | 19ad9ddb519084a75b48fe56bba24e67500e7ca3 (patch) | |
tree | f882c3a13fcabff6e5ad3f84a419021bbea8a3b8 /src | |
parent | 0a196017d8fbd76bc4a36c6452f3af7c3bb339d9 (diff) | |
parent | d6aadf7ade76b443c74e677ef5030c91b563ceab (diff) | |
download | coreclr-19ad9ddb519084a75b48fe56bba24e67500e7ca3.tar.gz coreclr-19ad9ddb519084a75b48fe56bba24e67500e7ca3.tar.bz2 coreclr-19ad9ddb519084a75b48fe56bba24e67500e7ca3.zip |
Merge pull request #2192 from dasMulli/random-seeding-improvements
System.Random: Parameterless constructor seeding improvement #1919
Diffstat (limited to 'src')
-rw-r--r-- | src/mscorlib/src/System/Random.cs | 58 |
1 files changed, 46 insertions, 12 deletions
diff --git a/src/mscorlib/src/System/Random.cs b/src/mscorlib/src/System/Random.cs index b8ff967a5f..4059b4cfde 100644 --- a/src/mscorlib/src/System/Random.cs +++ b/src/mscorlib/src/System/Random.cs @@ -11,13 +11,14 @@ ** ===========================================================*/ namespace System { - + using System; using System.Runtime; using System.Runtime.CompilerServices; using System.Globalization; using System.Diagnostics.Contracts; -[System.Runtime.InteropServices.ComVisible(true)] + + [System.Runtime.InteropServices.ComVisible(true)] [Serializable] public class Random { // @@ -46,11 +47,17 @@ namespace System { // // Constructors // - + + /*========================================================================================= + **Action: Initializes a new instance of the Random class, using a default seed value + ===========================================================================================*/ public Random() - : this(Environment.TickCount) { + : this(GenerateSeed()) { } - + + /*========================================================================================= + **Action: Initializes a new instance of the Random class, using a specified seed value + ===========================================================================================*/ public Random(int Seed) { int ii = 0; int mj, mk; @@ -79,11 +86,11 @@ namespace System { inextp = 21; Seed = 1; } - + // // Package Private Methods // - + /*====================================Sample==================================== **Action: Return a new random number [0..1) and reSeed the Seed array. **Returns: A double [0..1) @@ -117,11 +124,41 @@ namespace System { return retVal; } + + [ThreadStatic] + private static Random t_threadRandom; + private static readonly Random s_globalRandom = new Random(GenerateGlobalSeed()); + + /*=====================================GenerateSeed===================================== + **Returns: An integer that can be used as seed values for consecutively + creating lots of instances on the same thread within a short period of time. + ========================================================================================*/ + private static int GenerateSeed() { + Random rnd = t_threadRandom; + if (rnd == null) { + int seed; + lock (s_globalRandom) { + seed = s_globalRandom.Next(); + } + rnd = new Random(seed); + t_threadRandom = rnd; + } + return rnd.Next(); + } + + /*==================================GenerateGlobalSeed==================================== + **Action: Creates a number to use as global seed. + **Returns: An integer that is safe to use as seed values for thread-local seed generators. + ==========================================================================================*/ + private static int GenerateGlobalSeed() { + return Guid.NewGuid().GetHashCode(); + } + // // Public Instance Methods // - - + + /*=====================================Next===================================== **Returns: An int [0..Int32.MaxValue) **Arguments: None @@ -210,7 +247,4 @@ namespace System { } } } - - - } |