From 94de9cddf38500d5769fe8c240cdca84850d6956 Mon Sep 17 00:00:00 2001 From: umutKaracelebi <192463407+umutKaracelebi@users.noreply.github.com> Date: Sun, 29 Nov 2026 23:22:50 +0000 Subject: [PATCH] Fix inverted TRRANDOM type codes for gaussian and exponential random sources RandomMixin.format_spice_parameters mapped 'exponential' to TRRANDOM type 2 and 'gaussian' to type 3, but ngspice defines type 2 as gaussian and type 3 as exponential (as documented in the class docstring). A RandomVoltageSource / RandomCurrentSource declared as 'gaussian' therefore emitted exponential strictly-positive variates, and vice versa. Swap the two codes and add a regression test covering all four random types. --- PySpice/Spice/HighLevelElement.py | 4 ++-- unit-test/Spice/test_HighLevelElement.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/PySpice/Spice/HighLevelElement.py b/PySpice/Spice/HighLevelElement.py index dbce6238a..77eb9fc4a 100644 --- a/PySpice/Spice/HighLevelElement.py +++ b/PySpice/Spice/HighLevelElement.py @@ -557,9 +557,9 @@ def format_spice_parameters(self): if self.random_type == 'uniform': random_type = 1 - elif self.random_type == 'exponential': - random_type = 2 elif self.random_type == 'gaussian': + random_type = 2 + elif self.random_type == 'exponential': random_type = 3 elif self.random_type == 'poisson': random_type = 4 diff --git a/unit-test/Spice/test_HighLevelElement.py b/unit-test/Spice/test_HighLevelElement.py index e61769ef6..173d3c20f 100644 --- a/unit-test/Spice/test_HighLevelElement.py +++ b/unit-test/Spice/test_HighLevelElement.py @@ -71,6 +71,21 @@ def test(self): 'Vpwl1 1 0 DC 50V PWL(0s 0V 10ms 0V 11ms 5V 20ms 5V r=12ms td=34ms)', ) + ############################################## + + def test_random_source_type_codes(self): + + # ngspice TRRANDOM TYPE codes: 1 uniform, 2 gaussian, 3 exponential, 4 poisson + for random_type, code in (('uniform', 1), ('gaussian', 2), + ('exponential', 3), ('poisson', 4)): + self._test_spice_declaration( + RandomVoltageSource( + Circuit(''), 'rand1', '1', '0', + random_type=random_type, duration=1@u_ms, + ), + 'Vrand1 1 0 TRRANDOM({} 1ms 0s 1 0)'.format(code), + ) + #################################################################################################### if __name__ == '__main__':