Skip to content

Commit

Permalink
feat: improve random range
Browse files Browse the repository at this point in the history
  • Loading branch information
adhtruong committed Mar 1, 2025
1 parent 8c1fe02 commit 7d1e2b6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
7 changes: 5 additions & 2 deletions polyfactory/value_generators/primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ def create_random_float(
:returns: A random float.
"""
if minimum is None:
minimum = float(random.randint(0, 100)) if maximum is None else float(maximum) - 100.0
if maximum is None:
minimum = float(random.randint(0, 100))
else:
minimum = float(maximum) / 2 if maximum >= 0 else float(maximum) * 2.0
if maximum is None:
maximum = float(minimum) + 1.0 * 2.0 if minimum >= 0 else float(minimum) + 1.0 / 2.0
maximum = (float(minimum) + 1.0) * 2.0 if minimum >= 0 else (float(minimum) + 1.0) / 2.0
return random.uniform(float(minimum), float(maximum))


Expand Down
20 changes: 6 additions & 14 deletions tests/constraints/test_int_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,17 +205,9 @@ def test_handle_constrained_int_handles_multiple_of_with_ge_and_le(val1: int, va

def test_constraint_randomness() -> None:
random = Random(10)
result = handle_constrained_int(
random=random,
)
assert result == 55

result = handle_constrained_int(
random=random,
)
assert result == 61

result = handle_constrained_int(
random=random,
)
assert result == 85
assert [
handle_constrained_int(
random=random,
)
for _ in range(3)
] == [81, 109, 152]

0 comments on commit 7d1e2b6

Please sign in to comment.