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

Implement random.seed() #2318

merged 2 commits into from
Oct 17, 2023

Conversation

OmarMesqq
Copy link
Contributor

@OmarMesqq OmarMesqq commented Sep 10, 2023

Fixes #2110.

src/libasr/rng.cpp Outdated Show resolved Hide resolved
@OmarMesqq
Copy link
Contributor Author

@certik Can I use OS supplied PRNGs? I was thinking of writing the generator on my own, but it wouldn't be as cryptographically secure as /dev/urandom for instance

@certik
Copy link
Contributor

certik commented Sep 16, 2023

What issue is this trying to fix?

I would use the libc rand() and also allow to initialize it.

@OmarMesqq
Copy link
Contributor Author

@certik
it fixes the blocker reported by @rebcabin
With this final commit, running:

from random import random 
print(random())

yields a different random number every run

@OmarMesqq OmarMesqq marked this pull request as ready for review September 23, 2023 15:49
Copy link
Contributor

@certik certik left a comment

Choose a reason for hiding this comment

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

Perfect, thanks! I left some comments.

@certik certik marked this pull request as draft September 23, 2023 19:48
@OmarMesqq OmarMesqq marked this pull request as ready for review October 12, 2023 22:31
@certik
Copy link
Contributor

certik commented Oct 13, 2023

Thanks. I think this approach might work, I'll play with this soon.

@certik
Copy link
Contributor

certik commented Oct 17, 2023

We have to be 100% compatible with CPython. In CPython, random.seed(0) initializes the seed, so the following returns the same numbers in CPython:

    random.seed(0)
    print(random.random())
    random.seed(0)
    print(random.random())

But two different number in LPython (since 0 is used for clock initialization).

@certik
Copy link
Contributor

certik commented Oct 17, 2023

@OmarMesqq I fixed it up, let's see if this works.

@certik certik changed the title Feat ✨ improve random seed entropy Implement random.seed() Oct 17, 2023
@certik certik enabled auto-merge October 17, 2023 08:18
@certik certik merged commit 8aaa4a7 into lcompilers:main Oct 17, 2023
13 checks passed
@OmarMesqq
Copy link
Contributor Author

@certik Nice! Sorry, I didn't create more thorough tests. Already paying attention to the new bug issue you opened. Thanks Ondrej!

@OmarMesqq OmarMesqq deleted the feat/improve-random-seed-entropy branch October 17, 2023 12:54
@certik
Copy link
Contributor

certik commented Oct 17, 2023

Thank you!

Comment on lines +75 to +80
assert t1 != t2
assert t1 == t3
assert t1 != t4
assert t1 != t5
assert t4 == t5
assert t6 != t7
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BLOCKER: How to seed randoms with entropy ?
3 participants