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

The random numbers do not seem to work #2382

Closed
certik opened this issue Oct 17, 2023 · 0 comments · Fixed by #2464
Closed

The random numbers do not seem to work #2382

certik opened this issue Oct 17, 2023 · 0 comments · Fixed by #2464
Labels
bug Something isn't working

Comments

@certik
Copy link
Contributor

certik commented Oct 17, 2023

With #2318 we can now set the seed. The following program:

import random

def test_seed():
    random.seed()
    t1: f64 = random.random()
    print(t1)

test_seed()

prints:

(lp) repos/lpython(feat/improve-random-seed-entropy) $ lpython a.py
1.61927579977515883e-02
(lp) repos/lpython(feat/improve-random-seed-entropy) $ lpython a.py
1.59736196584876720e-02
(lp) repos/lpython(feat/improve-random-seed-entropy) $ lpython a.py
1.56996967344077767e-02
(lp) repos/lpython(feat/improve-random-seed-entropy) $ lpython a.py
1.55901275647758169e-02
(lp) repos/lpython(feat/improve-random-seed-entropy) $ lpython a.py
1.55040375029221345e-02
(lp) repos/lpython(feat/improve-random-seed-entropy) $ lpython a.py
1.54570792873655813e-02
(lp) repos/lpython(feat/improve-random-seed-entropy) $ lpython a.py
1.55588220877381153e-02
(lp) repos/lpython(feat/improve-random-seed-entropy) $ lpython a.py
1.63258062751618256e-02
(lp) repos/lpython(feat/improve-random-seed-entropy) $ lpython a.py
1.54414265488467296e-02
(lp) repos/lpython(feat/improve-random-seed-entropy) $ lpython a.py
1.55353429799598378e-02
(lp) repos/lpython(feat/improve-random-seed-entropy) $ lpython a.py
1.64823336603503352e-02

Those are not random numbers. The implementation is:

LFORTRAN_API double _lfortran_random()
{
    return (rand() / (double) RAND_MAX);
}

Which seems correct at first sight, but something somehow gets screwed up.

We should add a test for some statistics about random numbers, with high enough tolerance to always pass (say probability of failing 10e-10, or something like that), but fail on obvious cases like the above.

It looks like we should implement a Kolmogorov-Smirnov test, and we can use SciPy's kstest to check that we implemented it correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant