diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..37d2264 --- /dev/null +++ b/__init__.py @@ -0,0 +1,12 @@ +from .random_integer_node_efficient import RandomIntegerNodeEfficient +from .random_integer_node_list import RandomIntegerNodeList + +NODE_CLASS_MAPPINGS = { + "RandomIntegerNodeEfficient": RandomIntegerNodeEfficient, + "RandomIntegerNodeList": RandomIntegerNodeList, +} + +NODE_DISPLAY_NAME_MAPPINGS = { + "RandomIntegerNodeEfficient": "Efficient Random Integer Generator", + "RandomIntegerNodeList": "Random Integer Generator Using List", +} \ No newline at end of file diff --git a/random_integer_node_efficient.py b/random_integer_node_efficient.py new file mode 100644 index 0000000..0389ac0 --- /dev/null +++ b/random_integer_node_efficient.py @@ -0,0 +1,50 @@ +import random +import math + +class RandomIntegerNodeEfficient: + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "min_value": ("INT", {"default": 0}), + "max_value": ("INT", {"default": 100}), + "divisor": ("INT", {"default": 1}), + } + } + + RETURN_TYPES = ("INT",) + FUNCTION = "generate_random_integer" + CATEGORY = "Custom/Random" + + def generate_random_integer(self, min_value, max_value, divisor): + # Validate inputs + if min_value > max_value: + raise ValueError("min_value should not be greater than max_value.") + if divisor <= 0: + raise ValueError("divisor must be a positive integer.") + + # Calculate the smallest multiple of divisor >= min_value + if min_value % divisor == 0: + min_multiple = min_value + else: + min_multiple = min_value + (divisor - (min_value % divisor)) + + # Calculate the largest multiple of divisor <= max_value + max_multiple = max_value - (max_value % divisor) + + # Check if there are any multiples in the range + if min_multiple > max_multiple: + raise ValueError(f"No multiples of {divisor} within the range [{min_value}, {max_value}].") + + # Calculate the number of possible multiples + num_multiples = ((max_multiple - min_multiple) // divisor) + 1 + + # Select a random multiple + random_index = random.randint(0, num_multiples - 1) + random_number = min_multiple + (random_index * divisor) + + return (random_number,) + + @classmethod + def IS_CHANGED(cls, min_value, max_value, divisor): + return float('nan') diff --git a/random_integer_node_list.py b/random_integer_node_list.py new file mode 100644 index 0000000..0a97f3e --- /dev/null +++ b/random_integer_node_list.py @@ -0,0 +1,41 @@ +import random + +class RandomIntegerNodeList: + @classmethod + def INPUT_TYPES(cls): + return { + "required": { + "min_value": ("INT", {"default": 0}), + "max_value": ("INT", {"default": 100}), + "divisor": ("INT", {"default": 1}), + } + } + + RETURN_TYPES = ("INT",) + FUNCTION = "generate_random_integer" + CATEGORY = "Custom/Random" + + def generate_random_integer(self, min_value, max_value, divisor): + # Basic checks to avoid nonsensical parameters + if min_value > max_value: + raise ValueError("Minimum value should not be greater than maximum value.") + if divisor <= 0: + raise ValueError("Divisor should be a positive integer.") + + # Find the smallest multiple of 'divisor' at or above min_value + start = min_value + (-min_value % divisor) + # Find the largest multiple of 'divisor' at or below max_value + end = max_value - (max_value % divisor) + + # If there's no valid multiple within the range, handle accordingly + if start > end: + raise ValueError("No multiples of the divisor exist within the given range.") + + # Build a list of valid multiples + valid_multiples = list(range(start, end + divisor, divisor)) + # Randomly pick one + return (random.choice(valid_multiples),) + + @classmethod + def IS_CHANGED(cls, *args, **kwargs): + return float('nan')