mathy/seed_start.py
2025-11-05 10:11:35 +01:00

99 lines
3.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
Ultra-fast seed generator for mathstream start.txt.
Usage:
python seed_start.py --seed 10 --mode huge
Modes:
ur = /dev/urandom (1 byte per step)
ran = Python random.randint(0,255)
asc = random printable ASCII ord()
seq = deterministic sequence 0255 loop
huge = massive random digit chunks (SSD-limited chaos)
"""
import argparse
import random
import time
from pathlib import Path
from mathstream import StreamNumber, add, clear_logs
from tqdm import tqdm
def archive_start_file(start_path: Path):
"""Archive old start.txt and reset to 0."""
if start_path.exists():
timestamp = int(time.time())
backup = start_path.with_name(f"start.{timestamp}.txt")
backup.write_text(start_path.read_text())
start_path.write_text("0")
def seed_once(start_path: Path, byte_val: str):
"""Add a single number (string form) to start.txt using mathstream (streamed)."""
current = StreamNumber(start_path)
delta = StreamNumber(literal=byte_val)
result = add(current, delta)
new_value = "".join(result.stream())
start_path.write_text(new_value)
def fast_huge_random_string(size_bytes=65536):
"""Return a huge decimal string generated from /dev/urandom bytes."""
with open("/dev/urandom", "rb") as rnd:
chunk = rnd.read(size_bytes)
# Convert to digits quickly
digits = ''.join(str(b % 10) for b in chunk)
# Trim leading zeros so mathstream doesnt choke on '00000'
return digits.lstrip('0') or "0"
def main():
parser = argparse.ArgumentParser(description="Fast seeding for start.txt using mathstream")
parser.add_argument("--seed", type=int, required=True, help="number of random additions")
parser.add_argument("--mode", choices=["ur", "ran", "asc", "seq", "huge"], default="ur", help="random mode")
parser.add_argument("--chunk", type=int, default=65536,
help="bytes per chunk for huge mode (default 64KB)")
args = parser.parse_args()
start_path = Path("start.txt")
clear_logs()
archive_start_file(start_path)
print(f"Seeding {args.seed} iterations with mode '{args.mode}'")
seq_val = 0
if args.mode == "ur":
with open("/dev/urandom", "rb") as rnd:
for _ in tqdm(range(args.seed), desc="Seeding", unit="byte", ncols=80):
byte_val = rnd.read(1)[0]
seed_once(start_path, str(byte_val))
elif args.mode == "ran":
for _ in tqdm(range(args.seed), desc="Seeding", unit="val", ncols=80):
seed_once(start_path, str(random.randint(0, 255)))
elif args.mode == "asc":
printable = [chr(i) for i in range(32, 127)]
for _ in tqdm(range(args.seed), desc="Seeding", unit="char", ncols=80):
seed_once(start_path, str(ord(random.choice(printable))))
elif args.mode == "seq":
for _ in tqdm(range(args.seed), desc="Seeding", unit="seq", ncols=80):
seed_once(start_path, str(seq_val))
seq_val = (seq_val + 1) % 256
elif args.mode == "huge":
for _ in tqdm(range(args.seed), desc="Seeding", unit="huge", ncols=80):
huge_str = fast_huge_random_string(args.chunk)
seed_once(start_path, huge_str)
print(f"\nFinal start.txt value: {start_path.read_text().strip()}")
if __name__ == "__main__":
main()