From 6d1bc6c08476c90107abd952a4333cfcb500ee6e Mon Sep 17 00:00:00 2001 From: Eric Gustin <34000337+EricGustin@users.noreply.github.com> Date: Wed, 6 Nov 2024 09:28:05 -0800 Subject: [PATCH] Random int and random float tools (#148) As requested by D&D fans --- toolkits/math/arcade_math/tools/random.py | 36 +++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 toolkits/math/arcade_math/tools/random.py diff --git a/toolkits/math/arcade_math/tools/random.py b/toolkits/math/arcade_math/tools/random.py new file mode 100644 index 00000000..e09ac2f6 --- /dev/null +++ b/toolkits/math/arcade_math/tools/random.py @@ -0,0 +1,36 @@ +import random +from typing import Annotated, Optional + +from arcade.sdk import tool + + +@tool +def generate_random_int( + min_value: Annotated[int, "The minimum value of the random integer"], + max_value: Annotated[int, "The maximum value of the random integer"], + seed: Annotated[ + Optional[int], + "The seed for the random number generator. If None, the current system time is used.", + ] = None, +) -> Annotated[int, "A random integer between min_value and max_value"]: + """Generate a random integer between min_value and max_value (inclusive).""" + if seed is not None: + random.seed(seed) + + return random.randint(min_value, max_value) # noqa: S311 + + +@tool +def generate_random_float( + min_value: Annotated[float, "The minimum value of the random float"], + max_value: Annotated[float, "The maximum value of the random float"], + seed: Annotated[ + Optional[int], + "The seed for the random number generator. If None, the current system time is used.", + ] = None, +) -> Annotated[float, "A random float between min_value and max_value"]: + """Generate a random float between min_value and max_value.""" + if seed is not None: + random.seed(seed) + + return random.uniform(min_value, max_value) # noqa: S311