#
Miscellaneous
#
coin
// usage
{{coin()}}
Flip a coin!
{{coin()}};
=> 'heads'
{{coin()}};
=> 'tails'
#
dice
// usage
{{d4()}}
{{d6()}}
{{d8()}}
{{d10()}}
{{d12()}}
{{d20()}}
{{d30()}}
{{d100()}}
Return a value equal to the roll of a die.
{{d20()}};
=> 13
{{d6()}};
=> 4
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
// Usage
chance.fileWithContent({
fileName: "AwesomeFile",
fileExtension: "gif",
fileSize: 1024})
Returns an object with fileData as a Buffer and file name as a String
// Returns
{
fileData: <Buffer 00 00 00... 1998 more bytes>,
fileName: 'zo.gif'
}
Random name and format
{{fileWithContent({fileSize: 1024})}}
Random format
chance.fileWithContent({
fileName: "AwesomeFile",
fileSize: 2048})
- Takes the optional fileName and fileExtension, otherwise generates from file()
- 0 Size files are ok
#
guid
// usage
{{guid()}}
{{guid({version: 5})}}
Return a random guid, version 5 by default.
{{guid()}};
=> 'f0d8368d-85e2-54fb-73c4-2d60374295e3'
{{guid({version: 4})}};
=> 'c71f58e3-34af-43c0-b405-2764d6947d21'
#
hash
// usage
{{hash()}}
{{hash({length: 25})}}
{{hash({casing: 'upper'})}}
Return a random hex hash
{{hash()}}
=> 'e5162f27da96ed8e1ae51def1ba643b91d2581d8'
By default, the hash is lowercase and 40 hex characters long (same as a git commit hash).
Optionally specify a length
{{hash({length: 15})}}
=> 'c28f57cb599ada4'
Optionally specify casing to get a hash with only uppercase letters rather than the default lowercase
{{hash({casing: 'upper'})}}
=> '3F2EB3FB85D88984C1EC4F46A3DBE740B5E0E56E'
// usage
{{cc_types()}}
{{mersenne_twister()}}
{{mersenne_twister(12345)}}
{{months()}}
{{name_prefixes()}}
{{provinces()}}
{{states()}}
{{street_suffix()}}
{{street_suffixes()}}
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.
// Return the list of cc types
{{cc_types()}}
=> [{name: "American Express", short_name: 'amex', prefix: '34', length: 15}, ...]
// Return a new Mersenne Twister
{{mersenne_twister()}}
=> [object Object] // An instance of a Mersenne Twister
// Optionally provide a seed for that twister
{{mersenne_twister(12345)}}
=> [object Object] // An instance of a twister with seed 12345
// Return our list of name prefixes
{{months()}};
=> [{name: 'January', short_name 'Jan', numeric: '01'}, ...]
// Return our list of name prefixes
{{name_prefixes()}};
=> [{name: 'Doctor', abbreviation: 'Dr.'}, {name: 'Miss', abbreviation: 'Miss'}, ...]
// Return the list of provinces
{{provinces()}};
=> [{name: 'Alberta', abbreviation: 'AB'}, {name: 'British Columbia', abbreviation: 'BC'}, ...]
// Return the list of states
{{states()}};
=> [{name: 'Alabama', abbreviation: 'AL'}, {name: 'Alaska', abbreviation: 'AK'}, ...]
// Return a random street suffix
{{street_suffix()}};
=> {name: 'Street', abbreviation: 'St'}
// Return the list of street suffixes
{{street_suffixes()}};
=> [{name: 'Avenue', abbreviation: 'Ave'}, {name: 'Boulevard', abbreviation: 'Blvd'}, ...]
#
n
// usage
{{n(chance.email, 5)}}
{{n(chance.email, 5, { domain: "socialradar.com" })}}
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:
{{n(chance.email, 5)}};
=> [ '[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]' ]
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:
{{n(chance.email, 5, { domain: "socialradar.com" })}}
=> [ '[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]' ]
Note, these items are not guaranteed to be unique. If that is the intent, see {{unique()
}}
#
normal
// usage
{{normal()}}
{{normal({mean: 100})}}
{{normal({mean: 100, dev: 15})}}
Return a normally-distributed random variate.
{{normal()}}
=> 0.4244767651300604
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.
// Notice, since no deviation was specified, using the default of `1`
{{normal({mean: 100})}}
=> 99.68352269988522
// For example, to get a random IQ (which by definition has a mean of 100
// and a standard deviation of 15)
{{normal({mean: 100, dev: 15})}}
=> 85.11040121833615
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
// usage
{{radio()}}
Generate a random radio call sign.
{{radio()}};
=> 'KCXW'
Optionally specify a side of the Mississippi River to limit stations to that side.
See K and W for more details
{{radio({side: 'east'})}};
=> 'WKOQ'
{{radio({side: 'east'})}};
=> 'WNOW'
#
rpg
// usage
{{rpg('#d#')}}
{{rpg('#d#', {sum: true})}}
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.
{{rpg('3d10')}};
=> [1, 6, 9]
{{rpg('5d6')}};
=> [3, 1, 2, 5, 2]
Optionally specify a sum be returned rather than an array of dice.
{{rpg('3d10', {sum: true})}};
=> 14
#
tv
// usage
{{tv()}}
{{tv({side: 'west'})}}
Generate a TV station call sign. This is an alias for radio()
since they both
follow the same rules.
{{radio()}};
=> 'WXTY'
Optionally specify a side of the Mississippi River to limit stations to that side.
{{radio({side: 'west'})}};
=> 'KCYL'
{{radio({side: 'west'})}};
=> 'KQDV'
#
unique
// usage
{{unique(chance.state, 5)}}
{{unique(chance.state, 5, { comparator: func })}}
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.
{{unique(chance.state, 5)}};
=> ["SC", "WA", "CO", "TX", "ND"]
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)}}.
chance.unique(chance.currency, 2, {
comparator: function(arr, val) {
return arr.reduce(function(acc, item) {
return acc || (item.code === val.code);
}, false);
}
});
=> [{ code: "KYF", name: "Cayman Islands Dollar" }, { code: "CDF", name: "Congo/Kinshasa Franc" }]
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.
{{unique(chance.integer, 10, {min: 0, max: 100})}};
=> [78, 49, 7, 87, 59, 89, 84, 62, 60, 63]
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)}}.
{{unique(chance.state, 55)}};
=> RangeError: Chance: num is likely too large for sample set
#
weighted
// usage
{{weighted(['a', 'b'], [100, 1])}}
{{weighted(['a', 'b', 'c', 'd'], [1, 2, 3, 4])}}
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:
{{weighted(['a', 'b'], [100, 1])}};
=> 'a'
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:
{{weighted(['a', 'b', 'c', 'd'], [1, 2, 3, 4])}};
=> 'c'
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.
{{weighted(['a', 'b', 'c', 'd'], [0.1, 0.2, 0.3, 0.4])}};
=> 'd'
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:
{{weighted(['a', 'b', 'c', 'd'], [1, 2, 3, 4])}};
{{weighted(['a', 'b', 'c', 'd'], [0.1, 0.2, 0.3, 0.4])}};
{{weighted(['a', 'b', 'c', 'd'], [100, 200, 300, 400])}};
{{weighted(['a', 'b', 'c', 'd'], [17, 34, 51, 68])}};
{{weighted(['a', 'b', 'c', 'd'], [0.17, 0.34, 0.51, 0.68])}};
Recall JavaScript has first class functions so you could do something like the following:
{{weighted([{{fbid, chance.twitter, chance.ip], [10, 5, 1])()}}}};
=> 10000345166213
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).