Skip to content

Commit

Permalink
now runs with the same seed always give the same result
Browse files Browse the repository at this point in the history
  • Loading branch information
n0lavar committed Jun 11, 2024
1 parent e66e937 commit 517d478
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 27 deletions.
3 changes: 2 additions & 1 deletion cp_red_npc_generator/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
PyInstaller
dataclass_wizard
PyInstaller
numpy
5 changes: 3 additions & 2 deletions cp_red_npc_generator/src/generate_ammo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
# -*- coding: utf-8 -*-

import logging
import random
from typing import List, Dict
import numpy as np

from item import Item, ItemType
from npc import Npc
Expand Down Expand Up @@ -88,7 +88,8 @@ def add_basic_ammo(ammo_type: str) -> bool:
add_basic_ammo("Bullets") or add_basic_ammo("Slugs") or add_basic_ammo("Arrows")

for _ in range(RANDOM_GENERATING_NUM_ATTEMPTS):
required_ammo_type, magazine_size = random.choice(list(required_ammo_types.items()))
required_ammo_type = list(required_ammo_types.keys())[np.random.choice(len(required_ammo_types))]
magazine_size = required_ammo_types[required_ammo_type]
try_add_ammo(required_ammo_type,
choose_exponential_random_element(preferred_ammo_modifications, True),
magazine_size)
Expand Down
4 changes: 2 additions & 2 deletions cp_red_npc_generator/src/generate_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import functools
import logging
import random
import numpy as np
from typing import List, Optional, Callable

from npc import Npc
Expand Down Expand Up @@ -93,7 +93,7 @@ def distribute_points(weights: List[float],
stats_data = load_data("configs/stats.json")

streetrat_stats_table: List[List[int]] = stats_data["streetrat_stats"][npc_template.role.name]
streetrat_chosen_table: List[int] = random.choice(streetrat_stats_table)
streetrat_chosen_table: List[int] = streetrat_stats_table[np.random.choice(len(streetrat_stats_table))]
stats_mean_clamped_distributed = distribute_points(streetrat_chosen_table,
npc_template.rank.stats_budget.generate(),
2,
Expand Down
8 changes: 4 additions & 4 deletions cp_red_npc_generator/src/generate_weapon.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import copy
import logging
import dataclass_wizard
import random
import numpy as np
from typing import List, Optional, Set, Tuple
from dataclasses import dataclass, field, replace

Expand All @@ -29,7 +29,7 @@ def pick_weapon(budget: int,
return None, 0

for _ in range(RANDOM_GENERATING_NUM_ATTEMPTS):
preferred_weapon: str = random.choice(list(preferred_weapons))
preferred_weapon: str = np.random.choice(sorted(list(preferred_weapons)))
for cyberware in installed_cyberware:
if preferred_weapon in cyberware.item.tags:
logging.debug(f"\t\tFound a cyberware that acts like a preferred weapon: {cyberware.item}")
Expand All @@ -46,7 +46,7 @@ def pick_weapon(budget: int,
case _:
raise AssertionError

preferred_quality: ItemQuality = random.choice(list(preferred_qualities))
preferred_quality: ItemQuality = np.random.choice(preferred_qualities)
logging.debug(f"\t\tChosen quality: {preferred_quality}")

preferred_weapon_item: ItemWithNames = next(w for w in all_weapons if preferred_weapon in w.tags)
Expand All @@ -70,7 +70,7 @@ def pick_weapon(budget: int,
weapon = replace(weapon_copy,
price=price,
quality=preferred_quality,
name=f"{random.choice(list(weapon_copy.possible_names))} ({weapon_copy.name})")
name=f"{np.random.choice(list(weapon_copy.possible_names))} ({weapon_copy.name})")

logging.debug(f"\t\tPicked weapon: {weapon}")
logging.debug(f"\t\tMoney left: {budget - price}")
Expand Down
4 changes: 2 additions & 2 deletions cp_red_npc_generator/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
# -*- coding: utf-8 -*-

import argparse
import random
import dataclass_wizard
import logging
import time
import sys
import numpy as np

from generate_ammo import generate_ammo
from generate_armor import generate_armor
Expand Down Expand Up @@ -45,7 +45,7 @@ def main(args) -> int:
seed = args.seed
else:
seed = int(time.time())
random.seed(seed)
np.random.seed(seed)

rank_data = dataclass_wizard.fromdict(Rank, next(r for r in ranks if r["name"] == args.rank))
role_data = dataclass_wizard.fromdict(Role, next(r for r in roles if r["name"] == args.role))
Expand Down
4 changes: 2 additions & 2 deletions cp_red_npc_generator/src/normal_distribution.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

import random
from dataclasses import dataclass
import numpy as np


@dataclass
Expand All @@ -11,4 +11,4 @@ class NormalDistribution:
standard_deviation: float = 0

def generate(self) -> float:
return random.gauss(mu=self.mean, sigma=self.standard_deviation)
return np.random.normal(self.mean, self.standard_deviation)
21 changes: 7 additions & 14 deletions cp_red_npc_generator/src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
# -*- coding: utf-8 -*-

import json
import random
import math

import numpy as np

RANDOM_GENERATING_NUM_ATTEMPTS: int = 200

Expand All @@ -12,18 +12,11 @@ def left_align(obj, offset: int = 0, char: str = "\t") -> str:
return char * offset + obj


def choose_exponential_random_element(elements, reverse: bool = False):
def generate_exponential_random(lambda_param):
u = random.random()
return -math.log(1 - u) / lambda_param

exp_values = [generate_exponential_random(1.0) for _ in elements]
total = sum(exp_values)
probabilities = [v / total for v in exp_values]
if reverse:
probabilities = [1.0 - p for p in probabilities]

return random.choices(elements, weights=probabilities, k=1)[0]
def choose_exponential_random_element(elements, reverse: bool = False, scale=1.0):
weights = np.random.exponential(scale, len(elements))
probabilities = weights / weights.sum()
index = np.random.choice(len(elements), p=list(reversed(probabilities)) if reverse else probabilities)
return elements[index]


def load_data(json_path: str):
Expand Down

0 comments on commit 517d478

Please sign in to comment.