Skip to content

Commit

Permalink
CHG: generate reproducable random data
Browse files Browse the repository at this point in the history
Set seed on startup and use 'random.randbytes()' instead of os.urandom
(on Python >= 3.9)
  • Loading branch information
Werner Damman committed Apr 30, 2024
1 parent 216e1f2 commit 23f229e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 5 additions & 0 deletions procsim/core/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import importlib
import json
import os
import random
import signal
import sys
from typing import List, Optional, Tuple
Expand Down Expand Up @@ -328,6 +329,10 @@ def parse_command_line():
def main():
task_filename, job_filename, config_filename, scenario_name, log_level, no_match_outputs = parse_command_line()
logger = Logger('', '', '', Logger.LEVELS, []) # Create temporary logger

# init random generator with fixed seed to ensure reproducibility.
random.seed(0)

try:
# Program terminate/interrupt, will raise an exception which in turn will
# result in a log message.
Expand Down
8 changes: 6 additions & 2 deletions procsim/flex/product_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import bisect
import datetime
import os
import random
import re
import shutil
from typing import Dict, Iterable, List, Optional, Tuple
Expand Down Expand Up @@ -202,12 +203,15 @@ def _generate_bin_file(self, file_path: str, size_mb: Optional[int]) -> None:
input_file.close()

# otherwise look at 'size' (default 0) and write random data
# use randbytes if available (Python >= 3.9), otherwise fall back to os.urandom
else:
size = size_mb * 2**20 if size_mb is not None else 0

while size > 0:
amount = min(size, CHUNK_SIZE)
output_file.write(os.urandom(max(amount, 0)))
if hasattr(random, 'randbytes'):
output_file.write(random.randbytes(amount))
else:
output_file.write(os.urandom(amount))
size -= amount

output_file.close()
Expand Down

0 comments on commit 23f229e

Please sign in to comment.