← 输入框|github.com|刷新
Skip to content

BLOCKER: How to seed randoms with entropy ? #2110

Closed

Description

@rebcabin

This is a real BLOCKER but I hope easy to fix ? I need randoms that change from run-to-run. Our LPython random produces the same result every run.

from random import random

print(random())  ########### PRODUCES SAME RESULT EVERY RUN ##########

# BUT, how to seed with time-of-day or internet noise
# or some other sort of variable entropy?

# NO WORK from random import seed

Activity

  1. added
    BLOCKER
    question
    on Jul 5, 2023
  2. certik commented on Jul 5, 2023

    @certik
    Contributor

    As a quick fix, let's do:

    from lpython import init_random
    from random import random
    init_random()
    print(random())
    

    Where the init_random function is our own function, doing nothing in CPython mode, but initializing the random number generator for LPython mode. That should unblock Brian.

    Later we can figure out whether to call init_random() automatically in the main program and whether to always call it, or only if the user calls random, etc. (I think CPython somehow initializes the random number generator automatically). That's lower priority.

  3. rebcabin commented on Jul 5, 2023

    @rebcabin
    ContributorAuthor

    Let init_random have an optional argument (or something like that) so I can seed the RNG for the cases where I do not want entropy, i.e., where I do want deterministic pseudorandoms.

  4. rebcabin commented on Jul 6, 2023

    @rebcabin
    ContributorAuthor
  5. OmarMesqq commented on Sep 9, 2023

    @OmarMesqq
    Contributor

    Hello, I was thinking of grabbing this issue, and I already thought of a way of doing so with C++ random. I already have the code inside libasr in rng.cpp, but can't link it correctly.

    I set the SRC in CMakeLists.txt to include my file and exported it with extern "C" , but still doesn't work.

    Anyone got any tips?

  6. certik commented on Sep 10, 2023

    @certik
    Contributor

    @OmarMesqq go ahead and submit a PR with what you have, and then we'll help improve it.

  7. removed
    BLOCKER
    on Sep 23, 2023
  8. certik commented on Sep 23, 2023

    @certik
    Contributor

    I think @rebcabin is not blocked by this anymore, so I removed the BLOCKER tag.

  9. certik commented on Oct 17, 2023

    @certik
    Contributor

    This issue will be fixed by #2318.

    There is another issue that the random numbers do not appear random, which is a separate issue: #2382.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    easy to fixquestion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions