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

Fix random.random() #2464

Merged
merged 4 commits into from
Jan 26, 2024
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
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ RUN(NAME elemental_11 LABELS cpython llvm c NOFAST)
RUN(NAME elemental_12 LABELS cpython llvm c NOFAST)
RUN(NAME elemental_13 LABELS cpython llvm c NOFAST)
RUN(NAME test_random LABELS cpython llvm NOFAST)
RUN(NAME test_random_02 LABELS cpython llvm NOFAST)
RUN(NAME test_os LABELS cpython llvm c NOFAST)
RUN(NAME test_builtin LABELS cpython llvm c)
RUN(NAME test_builtin_abs LABELS cpython llvm c)
Expand Down
4 changes: 3 additions & 1 deletion integration_tests/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@ def test_seed():
t5 = random.random()
random.seed()
t7: f64 = random.random()

print(t1, t2, t3, t4, t5, t6, t7)
assert t1 != t2
assert t1 == t3
assert t1 != t4
assert t1 != t5
assert t4 == t5
assert t6 != t7
# assert t6 != t7
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@certik I commented this out as we discussed in the meet.


def check():
test_random()
Expand Down
11 changes: 11 additions & 0 deletions integration_tests/test_random_02.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from lpython import f64
import random

def test_seed():
random.seed()
t1: f64 = random.random()
t2: f64 = random.random()
print(t1, t2)
assert abs(t1 - t2) > 1e-3

test_seed()
15 changes: 12 additions & 3 deletions src/libasr/runtime/lfortran_intrinsics.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,18 @@ LFORTRAN_API void _lfortran_init_random_seed(unsigned seed)

LFORTRAN_API void _lfortran_init_random_clock()
{
srand((unsigned int)clock());
unsigned int count;
#if defined(_MSC_VER)
count = (unsigned int)clock();
#else
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
count = (unsigned int)(ts.tv_nsec);
} else {
count = (unsigned int)clock();
}
#endif
srand(count);
}

LFORTRAN_API double _lfortran_random()
Expand Down Expand Up @@ -1884,12 +1895,10 @@ LFORTRAN_API double _lfortran_time()
}

LFORTRAN_API void _lfortran_sp_rand_num(float *x) {
srand(time(0));
*x = rand() / (float) RAND_MAX;
}

LFORTRAN_API void _lfortran_dp_rand_num(double *x) {
srand(time(0));
*x = rand() / (double) RAND_MAX;
}

Expand Down
Loading