Skip to content

The random seed is the same across invocations #2545

Closed
@certik

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions