Skip to content

Commit a38a715

Browse files
committed
test: use isolated RandomState in OpenBugs.Random tests
Replace global np.random.seed(42) with isolated rng = np.random.RandomState(42) to prevent test collisions when running in parallel. Also removed [NotInParallel] attribute since tests now use isolated state.
1 parent a18b594 commit a38a715

1 file changed

Lines changed: 30 additions & 31 deletions

File tree

test/NumSharp.UnitTest/OpenBugs.Random.cs

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ namespace NumSharp.UnitTest
1919
/// print(np.random.rand()) # etc.
2020
/// </code>
2121
/// </summary>
22-
[NotInParallel]
2322
public class OpenBugsRandom : TestClass
2423
{
2524
// ===== CRITICAL: RNG Algorithm Mismatch =====
@@ -37,8 +36,8 @@ public class OpenBugsRandom : TestClass
3736
[OpenBugs]
3837
public void Rand_Seed42_ShouldMatchNumPy()
3938
{
40-
np.random.seed(42);
41-
var result = np.random.rand(0L);
39+
var rng = np.random.RandomState(42);
40+
var result = rng.rand(0L);
4241

4342
// NumPy expected value
4443
const double expected = 0.3745401188473625;
@@ -56,8 +55,8 @@ public void Rand_Seed42_ShouldMatchNumPy()
5655
[Test]
5756
public void Rand5_Seed42_ShouldMatchNumPy()
5857
{
59-
np.random.seed(42);
60-
var result = np.random.rand(5L);
58+
var rng = np.random.RandomState(42);
59+
var result = rng.rand(5L);
6160

6261
// NumPy expected values
6362
var expected = new double[] {
@@ -86,8 +85,8 @@ public void Rand5_Seed42_ShouldMatchNumPy()
8685
[OpenBugs]
8786
public void Randn_Seed42_ShouldMatchNumPy()
8887
{
89-
np.random.seed(42);
90-
var result = np.random.randn(0L);
88+
var rng = np.random.RandomState(42);
89+
var result = rng.randn(0L);
9190

9291
// NumPy expected value
9392
const double expected = 0.4967141530112327;
@@ -106,8 +105,8 @@ public void Randn_Seed42_ShouldMatchNumPy()
106105
[Test]
107106
public void Randn5_Seed42_ShouldMatchNumPy()
108107
{
109-
np.random.seed(42);
110-
var result = np.random.randn(5L);
108+
var rng = np.random.RandomState(42);
109+
var result = rng.randn(5L);
111110

112111
var expected = new double[] {
113112
0.4967141530112327,
@@ -133,8 +132,8 @@ public void Randn5_Seed42_ShouldMatchNumPy()
133132
[Test]
134133
public void Randint_Seed42_ShouldMatchNumPy()
135134
{
136-
np.random.seed(42);
137-
var result = np.random.randint(0, 10);
135+
var rng = np.random.RandomState(42);
136+
var result = rng.randint(0, 10);
138137

139138
const int expected = 6;
140139
var actual = (int)result;
@@ -151,8 +150,8 @@ public void Randint_Seed42_ShouldMatchNumPy()
151150
[Test]
152151
public void Randint5_Seed42_ShouldMatchNumPy()
153152
{
154-
np.random.seed(42);
155-
var result = np.random.randint(0, 10, new Shape(5));
153+
var rng = np.random.RandomState(42);
154+
var result = rng.randint(0, 10, new Shape(5));
156155

157156
var expected = new int[] { 6, 3, 7, 4, 6 };
158157

@@ -172,8 +171,8 @@ public void Randint5_Seed42_ShouldMatchNumPy()
172171
[Test]
173172
public void Normal_Seed42_ShouldMatchNumPy()
174173
{
175-
np.random.seed(42);
176-
var result = np.random.normal(0, 1);
174+
var rng = np.random.RandomState(42);
175+
var result = rng.normal(0, 1);
177176

178177
const double expected = 0.4967141530112327;
179178
var actual = result.GetDouble(0);
@@ -190,8 +189,8 @@ public void Normal_Seed42_ShouldMatchNumPy()
190189
[Test]
191190
public void Uniform_Seed42_ShouldMatchNumPy()
192191
{
193-
np.random.seed(42);
194-
var result = np.random.uniform(0.0, 1.0, 1);
192+
var rng = np.random.RandomState(42);
193+
var result = rng.uniform(0.0, 1.0, 1);
195194

196195
const double expected = 0.3745401188473625;
197196
var actual = result.GetDouble(0);
@@ -208,8 +207,8 @@ public void Uniform_Seed42_ShouldMatchNumPy()
208207
[Test]
209208
public void Choice_Seed42_ShouldMatchNumPy()
210209
{
211-
np.random.seed(42);
212-
var result = np.random.choice(10);
210+
var rng = np.random.RandomState(42);
211+
var result = rng.choice(10);
213212

214213
const int expected = 6;
215214
var actual = result.GetInt32(0);
@@ -227,8 +226,8 @@ public void Choice_Seed42_ShouldMatchNumPy()
227226
[OpenBugs]
228227
public void Permutation_Seed42_ShouldMatchNumPy()
229228
{
230-
np.random.seed(42);
231-
var result = np.random.permutation(5);
229+
var rng = np.random.RandomState(42);
230+
var result = rng.permutation(5);
232231

233232
var expected = new int[] { 1, 4, 2, 0, 3 };
234233

@@ -252,8 +251,8 @@ public void Permutation_Seed42_ShouldMatchNumPy()
252251
[Test]
253252
public void Exponential_Seed42_ShouldMatchNumPy()
254253
{
255-
np.random.seed(42);
256-
var result = np.random.exponential(1);
254+
var rng = np.random.RandomState(42);
255+
var result = rng.exponential(1);
257256

258257
const double expected = 0.4692680899768591;
259258
var actual = result.GetDouble(0);
@@ -270,8 +269,8 @@ public void Exponential_Seed42_ShouldMatchNumPy()
270269
[Test]
271270
public void Poisson_Seed42_ShouldMatchNumPy()
272271
{
273-
np.random.seed(42);
274-
var result = np.random.poisson(5.0, 1);
272+
var rng = np.random.RandomState(42);
273+
var result = rng.poisson(5.0, 1);
275274

276275
const long expected = 5;
277276
var actual = result.GetInt64(0);
@@ -288,8 +287,8 @@ public void Poisson_Seed42_ShouldMatchNumPy()
288287
[Test]
289288
public void Binomial_Seed42_ShouldMatchNumPy()
290289
{
291-
np.random.seed(42);
292-
var result = np.random.binomial(10, 0.5, 1);
290+
var rng = np.random.RandomState(42);
291+
var result = rng.binomial(10, 0.5, 1);
293292

294293
const long expected = 4;
295294
var actual = result.GetInt64(0);
@@ -306,8 +305,8 @@ public void Binomial_Seed42_ShouldMatchNumPy()
306305
[Test]
307306
public void Beta_Seed42_ShouldMatchNumPy()
308307
{
309-
np.random.seed(42);
310-
var result = np.random.beta(0.5, 0.5);
308+
var rng = np.random.RandomState(42);
309+
var result = rng.beta(0.5, 0.5);
311310

312311
const double expected = 0.5992069666276891;
313312
var actual = result.GetDouble(0);
@@ -324,8 +323,8 @@ public void Beta_Seed42_ShouldMatchNumPy()
324323
[Test]
325324
public void Gamma_Seed42_ShouldMatchNumPy()
326325
{
327-
np.random.seed(42);
328-
var result = np.random.gamma(2, 1);
326+
var rng = np.random.RandomState(42);
327+
var result = rng.gamma(2, 1);
329328

330329
const double expected = 2.3936793898692366;
331330
var actual = result.GetDouble(0);

0 commit comments

Comments
 (0)