You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+15-2Lines changed: 15 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -52,7 +52,11 @@ returns a random `Number` in the range `(lo, hi)`
52
52
(that is, not containing either `lo` or `hi`)
53
53
with a uniform distribution.
54
54
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),
56
60
returns `lo` unless the `excludeMin` option is true;
57
61
otherwise, returns `hi` unless the `excludeMax` option is true;
58
62
otherwise, throws a {{RangeError}}.
@@ -73,7 +77,7 @@ Specifically:
73
77
74
78
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)).
75
79
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.)
77
81
4. If the `excludeMax` option is false,
78
82
and `lo + maxN*step` is not within `epsilon` of `hi`,
79
83
but `lo + (maxN+1)*step` is
@@ -88,6 +92,11 @@ Specifically:
88
92
> This `step`/`epsilon` behavior is taken directly from [CSS's `random()` function](https://drafts.csswg.org/css-values-5/#random).
89
93
> It's also [being proposed for `Iterator.range()`](https://github.com/tc39/proposal-iterator.range/issues/64#issuecomment-2881243363).
90
94
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
+
91
100
92
101
## `Random.range(...)` ##
93
102
@@ -125,6 +134,10 @@ it excludes `lo` and `hi`.
125
134
If the range thus contains no possible values,
126
135
throws a RangeError.
127
136
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
+
128
141
129
142
> [!NOTE]
130
143
> [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