Skip to content

Commit a8ce8a5

Browse files
committed
Minor cleanup and nits
1 parent 6310c20 commit a8ce8a5

File tree

3 files changed

+47
-34
lines changed

3 files changed

+47
-34
lines changed

src/libraries/System.Private.CoreLib/src/System/Random.LegacyImpl.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ public partial class Random
1717
/// </summary>
1818
private sealed class LegacyImpl : ImplBase
1919
{
20+
/// <summary>Thread-static instance used to seed any legacy implementations created with the default ctor.</summary>
2021
[ThreadStatic]
21-
private static Xoshiro128StarStarImpl? t_seedGenerator;
22+
private static ImplBase? t_seedGenerator;
2223

2324
/// <summary>Reference to the <see cref="Random"/> containing this implementation instance.</summary>
2425
/// <remarks>Used to ensure that any calls to other virtual members are performed using the Random-derived instance, if one exists.</remarks>
@@ -27,7 +28,7 @@ private sealed class LegacyImpl : ImplBase
2728
private int _inext;
2829
private int _inextp;
2930

30-
public LegacyImpl(Random parent) : this(parent, (t_seedGenerator ??= new()).Next())
31+
public LegacyImpl(Random parent) : this(parent, (t_seedGenerator ??= CreateDefaultImpl()).Next())
3132
{
3233
}
3334

src/libraries/System.Private.CoreLib/src/System/Random.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ public Random() =>
2424
// With no seed specified, if this is the base type, we can implement this however we like.
2525
// If it's a derived type, for compat we respect the previous implementation, so that overrides
2626
// are called as they were previously.
27-
_impl =
28-
GetType() != typeof(Random) ? new LegacyImpl(this) :
29-
IntPtr.Size == 8 ? new Xoshiro256StarStarImpl() :
30-
new Xoshiro128StarStarImpl();
27+
_impl = GetType() == typeof(Random) ? CreateDefaultImpl() : new LegacyImpl(this);
28+
29+
/// <summary>Creates the default, optimized implementation used for `new Random()`.</summary>
30+
private static ImplBase CreateDefaultImpl() => IntPtr.Size == 8 ?
31+
new Xoshiro256StarStarImpl() : // optimized for 64-bit
32+
new Xoshiro128StarStarImpl(); // optimized for 32-bit
3133

3234
/// <summary>Initializes a new instance of the Random class, using the specified seed value.</summary>
3335
/// <param name="Seed">

src/libraries/System.Runtime.Extensions/tests/System/Random.cs

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Collections.Generic;
55
using System.Globalization;
6+
using System.Linq;
67
using System.Reflection;
78
using System.Text;
89
using Xunit;
@@ -216,26 +217,26 @@ public void ExpectedValues(bool derived)
216217
// sequences to change, this test verifies the first few numbers for a few seeds.
217218
int[][] expectedValues = new int[][]
218219
{
219-
new int[] {1559595546, 1755192844, 1649316166, 1198642031, 442452829, 1200195957, 1945678308, 949569752, 2099272109, 587775847},
220-
new int[] {534011718, 237820880, 1002897798, 1657007234, 1412011072, 929393559, 760389092, 2026928803, 217468053, 1379662799},
221-
new int[] {1655911537, 867932563, 356479430, 2115372437, 234085668, 658591161, 1722583523, 956804207, 483147644, 24066104},
222-
new int[] {630327709, 1498044246, 1857544709, 426253993, 1203643911, 387788763, 537294307, 2034163258, 748827235, 815953056},
223-
new int[] {1752227528, 2128155929, 1211126341, 884619196, 25718507, 116986365, 1499488738, 964038662, 1014506826, 1607840008},
224-
new int[] {726643700, 610783965, 564707973, 1342984399, 995276750, 1993667614, 314199522, 2041397713, 1280186417, 252243313},
225-
new int[] {1848543519, 1240895648, 2065773252, 1801349602, 1964834993, 1722865216, 1276393953, 971273117, 1545866008, 1044130265},
226-
new int[] {822959691, 1871007331, 1419354884, 112231158, 786909589, 1452062818, 91104737, 2048632168, 1811545599, 1836017217},
227-
new int[] {1944859510, 353635367, 772936516, 570596361, 1756467832, 1181260420, 1053299168, 978507572, 2077225190, 480420522},
228-
new int[] {919275682, 983747050, 126518148, 1028961564, 578542428, 910458022, 2015493599, 2055866623, 195421134, 1272307474},
229-
new int[] {2041175501, 1613858733, 1627583427, 1487326767, 1548100671, 639655624, 830204383, 985742027, 461100725, 2064194426},
230-
new int[] {1015591673, 96486769, 981165059, 1945691970, 370175267, 368853226, 1792398814, 2063101078, 726780316, 708597731},
231-
new int[] {2137491492, 726598452, 334746691, 256573526, 1339733510, 98050828, 607109598, 992976482, 992459907, 1500484683},
232-
new int[] {1111907664, 1356710135, 1835811970, 714938729, 161808106, 1974732077, 1569304029, 2070335533, 1258139498, 144887988},
233-
new int[] {86323836, 1986821818, 1189393602, 1173303932, 1131366349, 1703929679, 384014813, 1000210937, 1523819089, 936774940},
234-
new int[] {1208223655, 469449854, 542975234, 1631669135, 2100924592, 1433127281, 1346209244, 2077569988, 1789498680, 1728661892},
235-
new int[] {182639827, 1099561537, 2044040513, 2090034338, 922999188, 1162324883, 160920028, 1007445392, 2055178271, 373065197},
236-
new int[] {1304539646, 1729673220, 1397622145, 400915894, 1892557431, 891522485, 1123114459, 2084804443, 173374215, 1164952149},
237-
new int[] {278955818, 212301256, 751203777, 859281097, 714632027, 620720087, 2085308890, 1014679847, 439053806, 1956839101},
238-
new int[] {1400855637, 842412939, 104785409, 1317646300, 1684190270, 349917689, 900019674, 2092038898, 704733397, 601242406},
220+
new int[] { 1559595546, 1755192844, 1649316166, 1198642031, 442452829, 1200195957, 1945678308, 949569752, 2099272109, 587775847 },
221+
new int[] { 534011718, 237820880, 1002897798, 1657007234, 1412011072, 929393559, 760389092, 2026928803, 217468053, 1379662799 },
222+
new int[] { 1655911537, 867932563, 356479430, 2115372437, 234085668, 658591161, 1722583523, 956804207, 483147644, 24066104 },
223+
new int[] { 630327709, 1498044246, 1857544709, 426253993, 1203643911, 387788763, 537294307, 2034163258, 748827235, 815953056 },
224+
new int[] { 1752227528, 2128155929, 1211126341, 884619196, 25718507, 116986365, 1499488738, 964038662, 1014506826, 1607840008 },
225+
new int[] { 726643700, 610783965, 564707973, 1342984399, 995276750, 1993667614, 314199522, 2041397713, 1280186417, 252243313 },
226+
new int[] { 1848543519, 1240895648, 2065773252, 1801349602, 1964834993, 1722865216, 1276393953, 971273117, 1545866008, 1044130265 },
227+
new int[] { 822959691, 1871007331, 1419354884, 112231158, 786909589, 1452062818, 91104737, 2048632168, 1811545599, 1836017217 },
228+
new int[] { 1944859510, 353635367, 772936516, 570596361, 1756467832, 1181260420, 1053299168, 978507572, 2077225190, 480420522 },
229+
new int[] { 919275682, 983747050, 126518148, 1028961564, 578542428, 910458022, 2015493599, 2055866623, 195421134, 1272307474 },
230+
new int[] { 2041175501, 1613858733, 1627583427, 1487326767, 1548100671, 639655624, 830204383, 985742027, 461100725, 2064194426 },
231+
new int[] { 1015591673, 96486769, 981165059, 1945691970, 370175267, 368853226, 1792398814, 2063101078, 726780316, 708597731 },
232+
new int[] { 2137491492, 726598452, 334746691, 256573526, 1339733510, 98050828, 607109598, 992976482, 992459907, 1500484683 },
233+
new int[] { 1111907664, 1356710135, 1835811970, 714938729, 161808106, 1974732077, 1569304029, 2070335533, 1258139498, 144887988 },
234+
new int[] { 86323836, 1986821818, 1189393602, 1173303932, 1131366349, 1703929679, 384014813, 1000210937, 1523819089, 936774940 },
235+
new int[] { 1208223655, 469449854, 542975234, 1631669135, 2100924592, 1433127281, 1346209244, 2077569988, 1789498680, 1728661892 },
236+
new int[] { 182639827, 1099561537, 2044040513, 2090034338, 922999188, 1162324883, 160920028, 1007445392, 2055178271, 373065197 },
237+
new int[] { 1304539646, 1729673220, 1397622145, 400915894, 1892557431, 891522485, 1123114459, 2084804443, 173374215, 1164952149 },
238+
new int[] { 278955818, 212301256, 751203777, 859281097, 714632027, 620720087, 2085308890, 1014679847, 439053806, 1956839101 },
239+
new int[] { 1400855637, 842412939, 104785409, 1317646300, 1684190270, 349917689, 900019674, 2092038898, 704733397, 601242406 },
239240
};
240241

241242
for (int seed = 0; seed < expectedValues.Length; seed++)
@@ -393,6 +394,11 @@ public void Xoshiro_AlgorithmBehavesAsExpected()
393394
// This test is validating implementation detail. If the algorithm used by `new Random()` is ever
394395
// updated, this test will need to be updated as well.
395396

397+
// One and only one of Xoshiro128StarStar and Xoshiro256StarStar should be in a given build.
398+
Type implType = typeof(Random)
399+
.GetNestedTypes(BindingFlags.NonPublic)
400+
.Single(t => t.Name.StartsWith("Xoshiro", StringComparison.Ordinal));
401+
396402
var randOuter = new Random();
397403
object randInner = randOuter.GetType().GetField("_impl", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(randOuter);
398404
Assert.NotNull(randInner);
@@ -409,6 +415,8 @@ public void Xoshiro_AlgorithmBehavesAsExpected()
409415

410416
if (IntPtr.Size == 8)
411417
{
418+
Assert.Contains("256", implType.Name);
419+
412420
// Example seeds from https://www.pcg-random.org/posts/a-quick-look-at-xoshiro256.html
413421
s0.SetValue(randInner, 0x01d353e5f3993bb0ul);
414422
s1.SetValue(randInner, 0x7b9c0df6cb193b20ul);
@@ -479,6 +487,8 @@ public void Xoshiro_AlgorithmBehavesAsExpected()
479487
}
480488
else
481489
{
490+
Assert.Contains("128", implType.Name);
491+
482492
s0.SetValue(randInner, 0x01d353e5u);
483493
s1.SetValue(randInner, 0x7b9c0df6u);
484494
s2.SetValue(randInner, 0xfdfcaa91u);
@@ -488,14 +498,14 @@ public void Xoshiro_AlgorithmBehavesAsExpected()
488498
randOuter.NextBytes(buffer);
489499
AssertExtensions.SequenceEqual(new byte[]
490500
{
491-
0xDD,0x20,0x3A,0x37,0xEB,0x6F,0xFD,0x50,0xA3,0x7B,0xCD,0x37,0xA8,0xAA,0x19,0xA8,
492-
0x22,0xD6,0x21,0x57,0x55,0xF3,0xA2,0x56,0x73,0x30,0x61,0xDE,0x62,0xD8,0x02,0xB9,
493-
0x5C,0xAE,0x3E,0x2D,0xC8,0xD6,0xBF,0x7D,0x6D,0x86,0xCE,0x95,0x3F,0x7C,0xF0,0x86,
494-
0x36,0x26,0xB8,0xA7,0x5C,0x80,0xC8,0xA7,0xAC,0x2C,0xE6,0x0E,0x25,0x6F,0xEB,0x04,
495-
0x22,0xDE,0xB4,0xB6,0x48,0xB2,0x07,0x79,0x09,0xA8,0xF6,0x42,0xA8,0x5C,0x3F,0xCE,
496-
0x11,0xE9,0x91,0x8B,0x17,0x48,0x0B,0xE1,0xEB,0x0A,0x89,0xC1,0x64,0x3B,0x58,0x76,
497-
0x30,0x53,0x67,0x13,0x68,0xAC,0xF3,0x5D,0x1B,0x84,0xF5,0x88,0x42,0xC7,0x45,0x74,
498-
0x65,0xB5,0x11,0xF2,0x0D,0x3F,0x62,0xC8,0x5C,0x7C,0x1C,0x35,0x34,0x2D,0xBC,0x9E,
501+
0xDD, 0x20, 0x3A, 0x37, 0xEB, 0x6F, 0xFD, 0x50, 0xA3, 0x7B, 0xCD, 0x37, 0xA8, 0xAA, 0x19, 0xA8,
502+
0x22, 0xD6, 0x21, 0x57, 0x55, 0xF3, 0xA2, 0x56, 0x73, 0x30, 0x61, 0xDE, 0x62, 0xD8, 0x02, 0xB9,
503+
0x5C, 0xAE, 0x3E, 0x2D, 0xC8, 0xD6, 0xBF, 0x7D, 0x6D, 0x86, 0xCE, 0x95, 0x3F, 0x7C, 0xF0, 0x86,
504+
0x36, 0x26, 0xB8, 0xA7, 0x5C, 0x80, 0xC8, 0xA7, 0xAC, 0x2C, 0xE6, 0x0E, 0x25, 0x6F, 0xEB, 0x04,
505+
0x22, 0xDE, 0xB4, 0xB6, 0x48, 0xB2, 0x07, 0x79, 0x09, 0xA8, 0xF6, 0x42, 0xA8, 0x5C, 0x3F, 0xCE,
506+
0x11, 0xE9, 0x91, 0x8B, 0x17, 0x48, 0x0B, 0xE1, 0xEB, 0x0A, 0x89, 0xC1, 0x64, 0x3B, 0x58, 0x76,
507+
0x30, 0x53, 0x67, 0x13, 0x68, 0xAC, 0xF3, 0x5D, 0x1B, 0x84, 0xF5, 0x88, 0x42, 0xC7, 0x45, 0x74,
508+
0x65, 0xB5, 0x11, 0xF2, 0x0D, 0x3F, 0x62, 0xC8, 0x5C, 0x7C, 0x1C, 0x35, 0x34, 0x2D, 0xBC, 0x9E,
499509
}, buffer);
500510

501511
Assert.Equal(1539844677, randOuter.Next());

0 commit comments

Comments
 (0)