#Miscellaneous
#coin
Flip a coin!
#dice
Return a value equal to the roll of a die.
These are just wrappers around natural() but are convenient for use by games.
They return values between 1 and the number after the d
, so {{d4()}}
returns 1, 2, 3, or 4, just like a 4 sided die would.
#File with content
Generates a file with random name, extention and data
Returns an object with fileData as a Buffer and file name as a String
Random name and format
Random format
- Takes the optional fileName and fileExtension, otherwise generates from file()
- 0 Size files are ok
#guid
Return a random guid, version 5 by default.
#hash
Return a random hex hash
By default, the hash is lowercase and 40 hex characters long (same as a git commit hash).
Optionally specify a length
Optionally specify casing to get a hash with only uppercase letters rather than the default lowercase
These aren't really hidden per se, but just utility methods intended to be used internally but exposed externally in case they're useful to anyone.
#n
Provide any function that generates random stuff (usually another Chance function) and a number and n()
will generate an array of items with a length matching the length you specified.
For example, to generate 5 email addresses:
Any options that would be sent to the random function can be added following the number.
For example, {{email()}}
has options which can be specified, so you can generate 5 emails with a known domain as follows:
Note, these items are not guaranteed to be unique. If that is the intent, see {{unique()
}}
#normal
Return a normally-distributed random variate.
By default this starts with a mean of 0
and a standard deviation of 1
which is the standard normal distribution.
Optionally specify a mean and/or deviation.
Used in combination with the above generators, this can be an extremely powerful way to get more realistic results as often "pure random" results fail to approximate the real world.
#radio
Generate a random radio call sign.
Optionally specify a side of the Mississippi River to limit stations to that side.
See K and W for more details
#rpg
Given an input looking like #d#, where the first ## is the number of dice to roll and the second ## is the max of each die, returns an array of dice values.
Optionally specify a sum be returned rather than an array of dice.
#tv
Generate a TV station call sign. This is an alias for radio()
since they both follow the same rules.
Optionally specify a side of the Mississippi River to limit stations to that side.
#unique
Provide any function that generates random stuff (usually another Chance function) and a number and unique()
will generate a random array of unique (not repeating) items with a length matching the one you specified.
This is helpful when there are a limited number of options and you want a bunch but want to ensure each is different.
Optionally specify the comparator used to determine whether a generated item is in the list of already generated items. By default the comparator just checks to see if the newly generated item is in the array of already generated items. This works for most simple cases (such as {{state()
) but will not work if the generated item is an object (because the Array.prototype.indexOf()
method will not work on an object since 2 objects will not be strictly equal, ===
, unless they are references to the same object)}}.
You can also specify any arbitrary options in this third argument and they'll be passed along to the method you specify as the first.
For example, let's say you want to retrieve 10 unique integers between 0 and 100. This is easily achievable by specifying chance.integer
as hte function, 10 as the number to retrieve, and a min/max in the options.
Note, there could be cases where it is impossible to generate the unique number. For example, if you choose {{state
as shown above as the random function and want say, 55 uniques, Chance will throw a RangeError because it is impossible to generate 55 uniques because there are only 51 states in the available pool (50 states plus the District of Columbia)}}.
#weighted
Provide an array of items, and another array of items specifying the relative weights and Chance will select one of those items, obeying the specified weight.
For example, the following code:
Will generate 'a'
100 times more often than 'b'
but still choose one or the other randomly.
The weights are all relative, so if you have more than just two it will ensure that all items are generated relative to all of the weights.
For example, the following code:
Will generate a letter from the array but will pick 'b'
twice as often as it picks 'a'
and will pick 'c'
three times as often as it picks 'a'
and will pick 'd'
four times as often as it will pick 'a'
and will pick 'd'
two times as often as it will pick 'b'
.
The weights can be whole numbers as shown above or fractions.
There is no requirement that the weights sum to anything in particular, they are all compared relative to each other so all of the following are equivalent:
Recall JavaScript has first class functions so you could do something like the following:
That will pick one of the Chance methods with the relative weights specified and then immediately invoke it, so it will return a random fbid twice as often as it will return a twitter handle (because 10/5 is 2) and an fbid 10 times more often than it will return a random ip address (because 10/1 is 10). It will return a random twitter handle 5 times more often than it will return an ip address (because 5/1 is 5).