Skip to content

Instantly share code, notes, and snippets.

@PsychedelicShayna
Created December 16, 2023 23:41
Show Gist options
  • Save PsychedelicShayna/a6d3032ab5b181a8e038e6ca1f341b38 to your computer and use it in GitHub Desktop.
Save PsychedelicShayna/a6d3032ab5b181a8e038e6ca1f341b38 to your computer and use it in GitHub Desktop.
Generates a random floating point zero, with N decimal places randomized between a lower (L) and upper (U) bound.
def zero_rand_places(n: int = 5, l: int = 4, u: int = 5) -> float:
'''
Generates a random floating point zero, with n decimal places between
0.1 and 0.9, with l and u clamping the lower and upper bounds of the
number, e.g. with l = 4, u = 5, n = 3: 0.400 <= R <= 0.599
'''
lb: int = 0.1 * 10**n
ub: int = 0.1 * 10 ** (n + 1) - 1
lc: int = lb * l
uc: int = ub - (ub - lb + 1 - lb * u)
return float(randint(lc, uc)) / (10**n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment