-
Notifications
You must be signed in to change notification settings - Fork 171
Seeded random.random() with system clock if it is left unseeded #2549
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
Conversation
src/runtime/random.py
Outdated
def random() -> f64: | ||
""" | ||
Returns a random floating point number in the range [0.0, 1.0) | ||
""" | ||
if not _seed_set: | ||
_lfortran_init_random_clock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's one way to do it, but a better way is to call the init_random
at the beginning of the program, not at every call. We have infrastructure for this, I think there is a function that is being called at the beginning, so we just need to call this from it.
Initially we can call it every time, later on we can update it to only call it if random
is used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I cannot seem to find this function. Where is it, and where is it being called from?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Search _lpython_set_argv
in libasr/codegen/asr_to_llvm.cpp
. It calls it at the beginning of the program. We need to hook this init_random
there as well.
@certik Is this approach okay? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please mark this as ready for review once it is ready!
Thanks for working on it.
src/runtime/random.py
Outdated
@@ -4,34 +4,43 @@ | |||
eps: f64 = 1e-16 | |||
|
|||
#: TODO: Call `log` from C directly until we fix the multiple import issue | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please remove all the changes in this file?
@Thirumalai-Shaktivel I've reverted the changes in |
I think the changes looks good to me. @Shaikh-Ubaid, Why don't you review this PR and share your thoughts? |
I am concerned about calling it every time even when |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks fine to me. Thanks for this!
Thanks for the review, Ubaid! |
Fixes #2545 .
Has a
_seed_set
variable which checks if the seed has been explicitly set by the user, else will set the default seed to the system clock.