Skip to content

Commit ebf3719

Browse files
committed
Commit to the lo/hi arg ordering having to match the step value, with omitted step being positive. Closes #17
1 parent 185ba90 commit ebf3719

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ returns a random `Number` in the range `(lo, hi)`
5252
(that is, not containing either `lo` or `hi`)
5353
with a uniform distribution.
5454

55-
If there are no floats between `lo` and `hi`,
55+
If `lo` is greater than `hi`,
56+
throws a {{RangeError}}.
57+
58+
If `lo` and `hi` are either equal or consecutive floats
59+
(such that there are no floats between them),
5660
returns `lo` unless the `excludeMin` option is true;
5761
otherwise, returns `hi` unless the `excludeMax` option is true;
5862
otherwise, throws a {{RangeError}}.
@@ -73,7 +77,7 @@ Specifically:
7377

7478
1. Let `epsilon` be a small value related to `step`, chosen to match [`Iterator.range` issue #64](https://github.com/tc39/proposal-iterator.range/issues/64#issuecomment-2881243363)).
7579
2. Let `minN` be 0 if the `excludeMin` option is false, or 1 if it's true.
76-
3. Let `maxN` be the largest integer such that `lo + maxN*step` is less than or equal to `hi`, if the `excludeMax` option is false, or less than `hi`, if it's true.
80+
3. Let `maxN` be the largest integer such that `lo + maxN*step` is less than or equal to `hi`, if the `excludeMax` option is false, or less than `hi`, if it's true. (Or the opposite comparisons, if `step` is negative.)
7781
4. If the `excludeMax` option is false,
7882
and `lo + maxN*step` is not within `epsilon` of `hi`,
7983
but `lo + (maxN+1)*step` is
@@ -88,6 +92,11 @@ Specifically:
8892
> This `step`/`epsilon` behavior is taken directly from [CSS's `random()` function](https://drafts.csswg.org/css-values-5/#random).
8993
> It's also [being proposed for `Iterator.range()`](https://github.com/tc39/proposal-iterator.range/issues/64#issuecomment-2881243363).
9094
95+
If `step` is positive, `lo` must be less than or equal to `hi`,
96+
or else a {{RangeError}} is thrown.
97+
If `step` is negative, `lo` must be greater than or equal to `hi`,
98+
or else a {{RangeError}} is thrown.
99+
91100

92101
## `Random.range(...)` ##
93102

@@ -125,6 +134,10 @@ it excludes `lo` and `hi`.
125134
If the range thus contains no possible values,
126135
throws a RangeError.
127136

137+
Has the same constraints on `lo` vs `hi` ordering,
138+
and the relationship with positive, negative, or omitted `step`,
139+
as `Random.number()`.
140+
128141

129142
> [!NOTE]
130143
> [Issue 19](https://github.com/tc39/proposal-random-functions/issues/19) discusses what algorithm to use. Current plan uses 2 64-bit chunks if the lo-hi range contains less than 2^63 values. If the range is larger, the algorithm uses approximately N+1 64-bit chunks, where N is the number of 64-bit chunks it takes to represent the range in the first place (with an ignorable chance of rejection, requiring another set of chunks). Either way, *every* int in the range is possible and has a uniform chance, unlike `Random.number(lo, hi, {step:1})` for large ranges. (For `.int()`, if the range leaves the safe integer range, the values have non-uniform chances but are relative to the number of integers they represent, so the statistics are as close to uniform as possible.)

0 commit comments

Comments
 (0)