Skip to content

Commit

Permalink
Support generating a random seed from the command-line arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
weilycoder committed Dec 16, 2024
1 parent ecfec8d commit 3730978
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion cyaron/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import cast, Any, Dict, Iterable, Tuple, Union
import sys
import random
from typing import List, Optional, cast, Any, Dict, Iterable, Tuple, Union

__all__ = [
"ati",
Expand Down Expand Up @@ -69,3 +71,24 @@ def unpack_kwargs(
f"{funcname}() got an unexpected keyword argument '{next(iter(kwargs.items()))[0]}'"
)
return rv


def get_seed_from_argv(argv: Optional[List[str]] = None):
"""
Calculate a random seed from the command-line arguments,
referencing the implementation of `testlib.h`, but with differing behavior.
https://github.com/MikeMirzayanov/testlib/blob/9ecb11126c16caeda2ba375e0084b3ddd03d4ace/testlib.h#L800
"""
seed = 3905348978240129619
for s in sys.argv[1:] if argv is None else argv:
for c in s:
seed = seed * 0x5DEECE66D + ord(c) + 0xB
seed &= 0xFFFFFFFFFFFF
seed += 0x88A12C38
return seed & 0xFFFFFFFFFFFF


def set_seed_from_argv(argv: Optional[List[str]] = None, version: int = 2):
"""Set the random seed from the command-line arguments."""
random.seed(get_seed_from_argv(argv), version)

0 comments on commit 3730978

Please sign in to comment.