From 47fbcedd68cbff0615d0b5670e65cefb0199c9db Mon Sep 17 00:00:00 2001 From: Scott Gasch Date: Sat, 18 Feb 2023 11:51:51 -0800 Subject: [PATCH] Adds execute_probabilistically. --- src/pyutils/misc_utils.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/pyutils/misc_utils.py b/src/pyutils/misc_utils.py index 632f179..0449cd5 100644 --- a/src/pyutils/misc_utils.py +++ b/src/pyutils/misc_utils.py @@ -5,6 +5,7 @@ """Miscellaneous utilities.""" import os +import random import sys @@ -24,11 +25,19 @@ def debugger_is_attached() -> bool: Returns: True if a debugger is attached, False otherwise. """ - gettrace = getattr(sys, 'gettrace', lambda: None) + gettrace = getattr(sys, "gettrace", lambda: None) return gettrace() is not None -if __name__ == '__main__': +def execute_probabilistically(probability_to_execute: float) -> bool: + """ + Returns: + True with a given probability. + """ + return random.uniform(0.0, 100.0) < probability_to_execute + + +if __name__ == "__main__": import doctest doctest.testmod() -- 2.47.1