diff --git a/integration_tests/test_random.py b/integration_tests/test_random.py index dc54956c54..d20f6286e0 100644 --- a/integration_tests/test_random.py +++ b/integration_tests/test_random.py @@ -53,13 +53,31 @@ def test_weibullvariate(): print(r) def test_seed(): + random.seed() + t6: f64 = random.random() random.seed(123) t1: f64 t1 = random.random() random.seed(321) t2: f64 t2 = random.random() + random.seed(123) + t3: f64 + t3 = random.random() + random.seed(0) + t4: f64 + t4 = random.random() + random.seed(0) + t5: f64 + t5 = random.random() + random.seed() + t7: f64 = random.random() assert t1 != t2 + assert t1 == t3 + assert t1 != t4 + assert t1 != t5 + assert t4 == t5 + assert t6 != t7 def check(): test_random() diff --git a/src/libasr/runtime/lfortran_intrinsics.c b/src/libasr/runtime/lfortran_intrinsics.c index 3e39fa6640..f01cb94550 100644 --- a/src/libasr/runtime/lfortran_intrinsics.c +++ b/src/libasr/runtime/lfortran_intrinsics.c @@ -96,13 +96,14 @@ LFORTRAN_API void _lfortran_random_number(int n, double *v) } } -LFORTRAN_API void _lfortran_init_random(unsigned seed) +LFORTRAN_API void _lfortran_init_random_seed(unsigned seed) { - if (seed == 0) { - srand((unsigned int)clock()); - } else { - srand(seed); - } + srand(seed); +} + +LFORTRAN_API void _lfortran_init_random_clock() +{ + srand((unsigned int)clock()); } LFORTRAN_API double _lfortran_random() diff --git a/src/libasr/runtime/lfortran_intrinsics.h b/src/libasr/runtime/lfortran_intrinsics.h index e009fcb632..3ea56e4991 100644 --- a/src/libasr/runtime/lfortran_intrinsics.h +++ b/src/libasr/runtime/lfortran_intrinsics.h @@ -67,7 +67,8 @@ typedef double _Complex double_complex_t; LFORTRAN_API double _lfortran_sum(int n, double *v); LFORTRAN_API void _lfortran_random_number(int n, double *v); -LFORTRAN_API void _lfortran_init_random(unsigned seed); +LFORTRAN_API void _lfortran_init_random_clock(); +LFORTRAN_API void _lfortran_init_random_seed(unsigned seed); LFORTRAN_API double _lfortran_random(); LFORTRAN_API int _lfortran_randrange(int lower, int upper); LFORTRAN_API int _lfortran_random_int(int lower, int upper); diff --git a/src/runtime/random.py b/src/runtime/random.py index fd0ed6f384..3e7367dc4e 100644 --- a/src/runtime/random.py +++ b/src/runtime/random.py @@ -32,15 +32,28 @@ def random() -> f64: def _lfortran_random() -> f64: pass +@overload +def seed() -> None: + """ + Initializes the random number generator. + """ + _lfortran_init_random_clock() + return + +@overload def seed(seed: i32) -> None: """ Initializes the random number generator. """ - _lfortran_init_random(seed) + _lfortran_init_random_seed(seed) return @ccall -def _lfortran_init_random(seed: i32) -> None: +def _lfortran_init_random_clock() -> None: + pass + +@ccall +def _lfortran_init_random_seed(seed: i32) -> None: pass def randrange(lower: i32, upper: i32) -> i32: