Constructor
new Random(seedopt)
Creates an independent seeded pseudo-random generator.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
seed |
number |
<optional> |
1 | Integer seed between 1 and 2147483646, inclusive. |
- Source:
Example
var random = new rune.util.Random(12345);
var column = random.randomInt(0, 11);
random.reset();
Members
(readonly) seed :number
The seed used to start the current sequence.
Type:
- number
- Source:
Methods
next() → {number}
Returns the next value, greater than or equal to zero and less than one.
- Source:
Returns:
A pseudo-random fraction.
- Type
- number
pick(array) → {*}
Selects an element without modifying the array. Empty arrays return undefined.
Arrays with fewer than two elements do not advance the random sequence.
Parameters:
| Name | Type | Description |
|---|---|---|
array |
Array | Elements to choose from. |
- Source:
Throws:
-
-
If array is not an Array.
- Type
- TypeError
-
-
-
If the array contains more than 2147483646 elements.
- Type
- RangeError
-
Returns:
The selected element, or undefined if the array is empty.
- Type
- *
random(minopt, maxopt) → {number}
Returns a floating-point value in the specified range.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
min |
number |
<optional> |
0 | Inclusive minimum. |
max |
number |
<optional> |
1 | Maximum; floating-point rounding may reach this value. |
- Source:
Throws:
-
If bounds or their difference are not finite, or min > max.
- Type
- RangeError
Returns:
A pseudo-random value, or min when both bounds are equal.
- Type
- number
randomInt(min, max) → {number}
Returns an integer in an inclusive range. Rejection sampling avoids bias
when the number of possible results does not divide the generator's range.
A rejected sample advances the sequence before trying again.
Parameters:
| Name | Type | Description |
|---|---|---|
min |
number | Inclusive minimum safe integer. |
max |
number | Inclusive maximum safe integer. |
- Source:
Throws:
-
If bounds are not safe integers, min > max, or the range contains more than 2147483646 integers.
- Type
- RangeError
Returns:
A pseudo-random integer.
- Type
- number
reset(seedopt) → {undefined}
Restarts the sequence with its current seed, or sets a new seed.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
seed |
number |
<optional> |
Integer seed between 1 and 2147483646, inclusive. |
- Source:
Throws:
-
If a supplied seed is outside the supported integer range.
- Type
- RangeError
Returns:
- Type
- undefined
shuffle(array) → {Array}
Shuffles an array in place using Fisher-Yates and returns the same array.
Elements are not cloned. Arrays with fewer than two elements do not advance
the random sequence. Copy the array first to preserve its original order.
Parameters:
| Name | Type | Description |
|---|---|---|
array |
Array | Writable array to shuffle. |
- Source:
Throws:
-
-
If array is not an Array.
- Type
- TypeError
-
-
-
If the array contains more than 2147483646 elements.
- Type
- RangeError
-
Returns:
The supplied array in its new order.
- Type
- Array
sign() → {number}
Returns -1 or 1 with equal probability using the seeded sequence.
Each call advances the sequence once.
- Source:
Returns:
A pseudo-random sign.
- Type
- number
weightedPick(items, weights) → {*}
Selects an item using relative weights without modifying either array.
Each weight must be finite and non-negative, and at least one must be positive.
Zero weights exclude items. Weights need not add up to one or one hundred.
If only one item has positive weight, the random sequence is not advanced.
Probabilities are limited by floating-point precision and generator resolution.
Parameters:
| Name | Type | Description |
|---|---|---|
items |
Array | Items to choose from. |
weights |
Array.<number> | Relative weights, one per item. |
- Source:
Throws:
-
-
If either argument is not an Array.
- Type
- TypeError
-
-
-
If arrays are empty, lengths differ, weights are invalid, or all weights are zero.
- Type
- RangeError
-
Returns:
The selected item.
- Type
- *