From 08021bb745abba2aa4852da44095e4aa8d0581ea Mon Sep 17 00:00:00 2001 From: kholdrex Date: Sat, 6 Jun 2026 14:15:08 -0500 Subject: [PATCH] test: use constructor seeds in model specs --- spec/irt_ruby/rasch_model_spec.rb | 15 +++++++-------- spec/irt_ruby/three_parameter_model_spec.rb | 15 +++++++-------- spec/irt_ruby/two_parameter_model_spec.rb | 15 +++++++-------- 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/spec/irt_ruby/rasch_model_spec.rb b/spec/irt_ruby/rasch_model_spec.rb index be9ddbd..94ce9d8 100644 --- a/spec/irt_ruby/rasch_model_spec.rb +++ b/spec/irt_ruby/rasch_model_spec.rb @@ -160,14 +160,12 @@ end end - context "Deterministic seed" do - it "produces consistent results with the same seed" do - srand(123) - model1 = described_class.new(data_array, max_iter: 200, learning_rate: 0.05) + context "Constructor seed" do + it "produces consistent results with the same seed option" do + model1 = described_class.new(data_array, max_iter: 200, learning_rate: 0.05, seed: 123) result1 = model1.fit - srand(123) - model2 = described_class.new(data_array, max_iter: 200, learning_rate: 0.05) + model2 = described_class.new(data_array, max_iter: 200, learning_rate: 0.05, seed: 123) result2 = model2.fit expect(result1[:abilities]).to eq(result2[:abilities]) @@ -179,10 +177,11 @@ it "handles a moderately large dataset without error" do n_examinees = 20 n_items = 10 + rng = Random.new(123) big_data = Array.new(n_examinees) do - Array.new(n_items) { rand < 0.5 ? 1 : 0 } + Array.new(n_items) { rng.rand < 0.5 ? 1 : 0 } end - model = described_class.new(big_data, max_iter: 300, learning_rate: 0.05) + model = described_class.new(big_data, max_iter: 300, learning_rate: 0.05, seed: 123) expect { model.fit }.not_to raise_error results = model.fit diff --git a/spec/irt_ruby/three_parameter_model_spec.rb b/spec/irt_ruby/three_parameter_model_spec.rb index 786a899..a0b6178 100644 --- a/spec/irt_ruby/three_parameter_model_spec.rb +++ b/spec/irt_ruby/three_parameter_model_spec.rb @@ -210,14 +210,12 @@ end end - context "Deterministic seed" do - it "produces consistent results with the same seed" do - srand(123) - model1 = described_class.new(data_array, max_iter: 200, learning_rate: 0.05) + context "Constructor seed" do + it "produces consistent results with the same seed option" do + model1 = described_class.new(data_array, max_iter: 200, learning_rate: 0.05, seed: 123) result1 = model1.fit - srand(123) - model2 = described_class.new(data_array, max_iter: 200, learning_rate: 0.05) + model2 = described_class.new(data_array, max_iter: 200, learning_rate: 0.05, seed: 123) result2 = model2.fit expect(result1[:abilities]).to eq(result2[:abilities]) @@ -231,11 +229,12 @@ it "handles a moderately large dataset without error" do n_examinees = 20 n_items = 8 + rng = Random.new(123) big_data = Array.new(n_examinees) do - Array.new(n_items) { rand < 0.5 ? 1 : 0 } + Array.new(n_items) { rng.rand < 0.5 ? 1 : 0 } end - model = described_class.new(big_data, max_iter: 300, learning_rate: 0.05) + model = described_class.new(big_data, max_iter: 300, learning_rate: 0.05, seed: 123) expect { model.fit }.not_to raise_error results = model.fit diff --git a/spec/irt_ruby/two_parameter_model_spec.rb b/spec/irt_ruby/two_parameter_model_spec.rb index 9fb598e..fbc2a16 100644 --- a/spec/irt_ruby/two_parameter_model_spec.rb +++ b/spec/irt_ruby/two_parameter_model_spec.rb @@ -169,14 +169,12 @@ end end - context "Deterministic seed" do - it "yields consistent results with the same seed" do - srand(123) - model1 = described_class.new(data_array, max_iter: 200, learning_rate: 0.05) + context "Constructor seed" do + it "yields consistent results with the same seed option" do + model1 = described_class.new(data_array, max_iter: 200, learning_rate: 0.05, seed: 123) result1 = model1.fit - srand(123) - model2 = described_class.new(data_array, max_iter: 200, learning_rate: 0.05) + model2 = described_class.new(data_array, max_iter: 200, learning_rate: 0.05, seed: 123) result2 = model2.fit expect(result1[:abilities]).to eq(result2[:abilities]) @@ -189,11 +187,12 @@ it "handles a moderately sized dataset without error" do n_examinees = 20 n_items = 8 + rng = Random.new(123) big_data = Array.new(n_examinees) do - Array.new(n_items) { rand < 0.5 ? 1 : 0 } + Array.new(n_items) { rng.rand < 0.5 ? 1 : 0 } end - model = described_class.new(big_data, max_iter: 300, learning_rate: 0.05) + model = described_class.new(big_data, max_iter: 300, learning_rate: 0.05, seed: 123) expect { model.fit }.not_to raise_error results = model.fit