|
1 | 1 | import randint from './randint.js';
|
2 | 2 |
|
3 | 3 | /**
|
4 |
| - * Return a randomly selected element from range(start, stop, step). |
| 4 | + * Pick an element from range(start, stop, step) uniformly at random. |
| 5 | + * |
| 6 | + * Return an element from range(start, stop, step) selected uniformly at random. |
| 7 | + * If step is positive, this set corresponds to |
| 8 | + * {x: x in [start, stop[ AND x % step = 0}. |
| 9 | + * If step is negative, the range has to be given in reverse order, that is, |
| 10 | + * largest value first, smallest value second. Both the starting value and the |
| 11 | + * step value are optional. By default the starting value is <code>0</code>. |
| 12 | + * The default for the step value is <code>1</code>. |
| 13 | + * |
| 14 | + * TODO: Handle empty ranges. |
| 15 | + * |
| 16 | + * @param {number} [start=0] - The starting value. |
| 17 | + * @param {number} stop - The stopping value. |
| 18 | + * @param {number} [step=1] - The step value. |
| 19 | + * @return {number} The picked element. |
5 | 20 | */
|
6 |
| - |
7 | 21 | const randrange = (start, stop, step) => {
|
8 |
| - // TODO handle empty ranges |
9 |
| - |
10 | 22 | if (stop === undefined) return randint(0, start);
|
11 | 23 | if (step === undefined) step = 1;
|
12 | 24 |
|
|
0 commit comments