Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement random.seed() #2318

Merged
merged 2 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions integration_tests/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,33 @@ def test_weibullvariate():
r = random.weibullvariate(-5.6, 1.2)
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
Comment on lines +75 to +80
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are comparing floating point values here. I think we need to use range comparison abs(a - b) < 1e-5 here.


def check():
test_random()
test_randrange()
Expand All @@ -60,5 +87,6 @@ def check():
test_paretovariate()
test_expovariate()
test_weibullvariate()
test_seed()

check()
10 changes: 10 additions & 0 deletions src/libasr/runtime/lfortran_intrinsics.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ LFORTRAN_API void _lfortran_random_number(int n, double *v)
}
}

LFORTRAN_API void _lfortran_init_random_seed(unsigned seed)
{
srand(seed);
}

LFORTRAN_API void _lfortran_init_random_clock()
{
srand((unsigned int)clock());
}

LFORTRAN_API double _lfortran_random()
{
return (rand() / (double) RAND_MAX);
Expand Down
2 changes: 2 additions & 0 deletions src/libasr/runtime/lfortran_intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +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_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);
Expand Down
24 changes: 24 additions & 0 deletions src/runtime/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,30 @@ 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(seed)
return

@ccall
def _lfortran_init_random_clock() -> None:
pass

@ccall
def _lfortran_init_random_seed(seed: i32) -> None:
pass

def randrange(lower: i32, upper: i32) -> i32:
"""
Return a random integer N such that `lower <= N < upper`.
Expand Down
Loading