shuffle: add random seed env var

This commit is contained in:
Starbeamrainbowlabs 2023-05-11 15:59:01 +01:00
parent ac040717e6
commit 20092c6829
Signed by: sbrl
GPG key ID: 1BE5172E637709C2

View file

@ -1,5 +1,9 @@
import os
from copy import deepcopy from copy import deepcopy
from random import randint import random
from loguru import logger
def shuffle(lst): def shuffle(lst):
@ -9,10 +13,17 @@ def shuffle(lst):
@param lst list The list to shuffle. @param lst list The list to shuffle.
@return list The a new list that is a shuffled copy of the original. @return list The a new list that is a shuffled copy of the original.
""" """
tmplist = deepcopy(lst) tmplist = deepcopy(lst)
m = len(tmplist) m = len(tmplist)
if RANDSEED in os.environ:
seed = os.environ["RANDSEED"]
random.seed(seed)
logger.info(f"Random seed set to {seed}, first 3 values: {random.randint(0, m)}, {random.randint(0, m)}, {random.randint(0, m)}")
while (m): while (m):
m -= 1 m -= 1
i = randint(0, m) i = random.randint(0, m)
tmplist[m], tmplist[i] = tmplist[i], tmplist[m] tmplist[m], tmplist[i] = tmplist[i], tmplist[m]
return tmplist return tmplist