-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc15a73
commit 36689f6
Showing
6 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,10 @@ | |
subtract | ||
) | ||
|
||
from .number import ( | ||
random_ | ||
) | ||
|
||
from .predicates import ( | ||
is_array, | ||
is_boolean, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from __future__ import absolute_import | ||
|
||
import sys | ||
import random | ||
import math | ||
|
||
|
||
def random_(n1=None, n2=None): | ||
"""Returns a random integer/float from :attr:`n1` to :attr:`n2` (both | ||
inclusive) | ||
Args: | ||
n1 (int/float/str): Value given by the user. | ||
n2 (int/float/str): Value given by the user. | ||
Returns: | ||
int/float: Random integer/float value between :attr:`n1` and | ||
:attr:`n2`. | ||
Example: | ||
>>> result = random_(5, 6) | ||
>>> assert 5 <= result <= 6 | ||
>>> result = random_(5) | ||
>>> assert 0 <= result <= 5 | ||
.. versionadded:: TODO | ||
""" | ||
n1 = 0 if not n1 else n1 | ||
n2 = 0 if not n2 else n2 | ||
|
||
n1 = float(n1) | ||
n2 = float(n2) | ||
|
||
max_value = max(n1, n2) | ||
min_value = min(n1, n2) | ||
diff = max_value - min_value | ||
|
||
result = (random.random() * diff) + min_value | ||
|
||
if diff <= 1 or (diff > 1 and math.ceil(result) > max_value): | ||
return round(result, 2) | ||
else: | ||
return math.ceil(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from .fixtures import parametrize | ||
|
||
import sugar as _ | ||
import time | ||
|
||
|
||
@parametrize('args', [ | ||
(0, 5), | ||
(2, 5), | ||
(-6, 0), | ||
(5.5, 11.7), | ||
(5.5, 6.5) | ||
]) | ||
def test_random(args): | ||
assert args[0] <= _.random_(*args) <= args[1] |