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

The random seed is the same across invocations #2545

Closed

Description

@certik
import random

def test_seed():
    print(random.random())
    print(random.random())

test_seed()

This gives in LPython:

$ lpython c.py 
7.82636925942561089e-06
1.31537788143166245e-01
$ ./c.out 
7.82636925942561089e-06
1.31537788143166245e-01
$ ./c.out
7.82636925942561089e-06
1.31537788143166245e-01
$ ./c.out
7.82636925942561089e-06
1.31537788143166245e-01
$ ./c.out
7.82636925942561089e-06
1.31537788143166245e-01

Compared to CPython:

$ python c.py 
0.1609877035160111
0.6090058937273372
$ python c.py
0.8816157116238759
0.7730379472615071
$ python c.py
0.7851910501722984
0.926580268293728
$ python c.py
0.23538691043891513
0.3621541015534898
$ python c.py
0.09200106922546647
0.8737898472693616

The random number generator itself works well, but the seed is always the same. We need to initialize the seed using system clock at the beginning of the program.

Activity

  1. added
    bug
    on Feb 18, 2024
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions