Methods
(static) gaussian(stdev, meanopt, negativeopt)
- Description:
Generate a whole random number, on a normal (Gaussian, "bell curve") distribution. This random number generator will give more useful results than
random()if you do not want the numbers to be evenly distributed.
- Source:
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
stdev |
Number | The amount of variance from the mean, where about 68% of the numbers will be a number +/- this amount. |
||
mean |
Number |
<optional> |
0
|
The mean around which random numbers will be generated. |
negative |
Boolean |
<optional> |
false
|
should the returned value be negative? This is false by default and will only return positive numbers |
Returns:
a random number
(static) guid() → {String}
- Description:
Generates a unique string (not guaranteed to be globally unique, but certainly unique in any given context).
- Source:
Example
guid()
=> "6v2w2762322fwsds"
Returns:
a context unique string
- Type
- String
(static) random(value) → {Object}
- Description:
Return a random value from a "strategy" value, recursively. An element will be selected from an array, a function will be executed for its return value, and a string with variants will be returned with a variant selected. The value is then checked again to see if it can still be randomized further, and this process continues until a primitive value is returned (a number, object, string, or boolean).
- Source:
Example
random("A")
=> "A"
random(['A','B','C']);
=> 'A'
random("{Big|Bad|Black} Dog");
=> 'Bad Dog'
random(() => ["A","B","C"]);
=> "B"
Parameters:
| Name | Type | Description |
|---|---|---|
value |
String | Array | function | A string with optional variants, or an array from which to select an element, or a function that returns a value. |
Returns:
a single randomized instance, based on the value passed in
- Type
- Object
(static) randomElement(an)
- Description:
A function that takes an array and returns a random element in the array. Does not execute functions, select a string out of string expressions, or perform any other recursive randomization.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
an |
collection | array |
Returns:
one element of the array
(static) roll(number) → {Number}
- Description:
Returns a random number based on a die roll string or a provided number. Math operations are supported.
- Source:
Example
roll(8)
=> 4
Parameters:
| Name | Type | Description |
|---|---|---|
number |
Number | the maximum value to return |
Returns:
a value from 1 to N
- Type
- Number
(static) roll(notation) → {Number}
- Source:
Example
roll("3d6+2")
=> 9
roll("(2d4*10)+500")
=> 540
Parameters:
| Name | Type | Description |
|---|---|---|
notation |
String | a notation for a dice roll |
Returns:
a die roll value
- Type
- Number
(static) selectElements()
- Description:
This method takes a JSON description of how to select elements in a pseudo- random fashion. It returns an array of selected tokens.
tokens. A token may consist of the string to be returned, plus a cardinality, in the form "[cardinality]:[string]". The cardinality can be a number ("4") or a dice notation ("2d6"). This is how many times the token will appear in the output array, if it is selected. This happens any time we say the token is “added” to the array below.
rules. There are three: some, any, and all. If an object is passed to the method, these are the property names of the object. In order to repeat rules in this object, they can be postfixed with any value such as all-1” and “all-2”. Since each rule is a property, it is associated to a property value descrining the way to select elements from that value:
-
some selects some of the tokens from the array or object. The exact number is indicated by the rule, for example “some:2d3" or “some:2”. If it’s an array, tokens are randomly selected. If it's an object, the weighted selection is based on the object as described below. Percentages must add up to 100%.
-
one synonymous with “some:1”
-
any selects any of the tokens that match against a percentage test from an array or object. If it’s an array, this is functionally equivalent to “some:1”. If it’s an object, the items are tested in order against a random percentage and those that match are added to the output array. If the table uses rarity values, these are tested in order, a similar fashion, and added to the array. Percentages do not have to add up to 100%.
-
all add all tokens from an array to the output stream. If an object is associated to the rule, all the values are added and percentage chance or rarity are ignored.
Object tables. The keys of the object are tokens and the values are percentage values (of type number) or rarity values ("C", "U", "R"). The rule will select tokens from the object keys based on the values of the object (in effect this object forms a weighted table similar to the Table and RarityTable classes).
-
- Source:
Example
"3:Stall"
=> ["Stall", "Stall", "Stall"]
[ "A", "2:B", "1d3:C" ]
=> ["A", "B", "B", "C"]
=> ["A", "B", "B", "C", "C", "C"]
// "&" can be used to group tokens
{ "some:1": { "A & B": 50, "C": 50 } }
=> ["A", "B"]
=> ["C"]
// same result; the postfix allows properties under the same key
{ "some-2:1": { "D": 50, "E": 50 } }
=> ["E"]
{ "some:1": ["A", "B"] }
=> ["B"]
{ "some:1": [ "A", "2:B", "1d3:C" ] }
=> ["A"]
=> ["B", "B"]
=> ["C", "C", "C"]
// 1d2 elements, each added by percentage weight. must add up to 100%
{ "some:1d2": { "A": 20, "B": 20, "C": 60 } }
=> ["A"]
=> ["C", "B"]
=> ["C", "C"]
// common, uncommon, and rare
{ "some:1": { "A": "C", "B": "U", "C": "R"} }
=> ["A"]
{ "some:1d2": { "A": "C", "B": "U" } }
=> ["A"]
=> ["B", "A"]
=> ["B", "B"]
// all does *not* have to add up to 100%
{ "all": { "A": 20, "2d3:B": 75 } }
=> []
=> ["A"]
=> ["A", "B", "B"]
=> ["B", "B", "B", "B", "B", "B"]
// four times
{ "all": ["4:A"] }
=> ["A", "A", "A", "A"]
{ "all": ["1d3:D", "1d3:E", "1d3:F"] }
=> ["D","D","D","E","F","F","F"]
(static) selectElementsKeys(tree) → {Array.<String>}
- Description:
Extracts all possible element keys from a selection tree structure.
- Source:
Example
selectElementsKeys(["apple", "banana", "cherry"])
=> ["apple", "banana", "cherry"]
selectElementsKeys({ fruits: ["2d4:apple", "orange&pear"] })
=> ["apple", "orange", "pear"]
Parameters:
| Name | Type | Description |
|---|---|---|
tree |
Object | Array | string | The selection tree to extract keys from |
Returns:
Array of all possible element keys that could be selected
- Type
- Array.<String>
(static) test(percentage, value1opt, value2opt) → {Boolean|any}
- Description:
Test against a percentage that something will occur.
- Source:
Example
if (test(80)) {
// Happens 80% of the time.
}
test(15, 'A', 'B')
=> 'B'
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
percentage |
Number | The percentage chance that the function returns true |
|
value1 |
any |
<optional> |
the value to return if the test succeeds |
value2 |
any |
<optional> |
the value to return if the test fails |
Returns:
true if test passes, false otherwise
- Type
- Boolean | any