Whether this is the same as another object interfacting RngInterface, i.e. they will generate the same next number.
The thing to compare
Outputs what the distribution supports in terms of output
Takes a random key from an object with a key:number pattern
Using a Map allows objects to be specified as keys, can be useful for choosing between concrete objects.
The values to choose from
The random choice from the array
Will return:
rng.weightedChoice({
a: 1,
b: 2,
c: 3,
d: 4
});
Will return:
const diamond = new Item('diamond');
const ruby = new Item('ruby');
const pebble = new Item('pebble');
const choices = new Map();
choices.set(diamond, 1);
choices.set(ruby, 10);
choices.set(pebble, 100);
rng.weightedChoice(choices);
Generates a Gaussian normal value via Box–Muller transform
There are two ways of calling, either with an object with mean and stddev as keys, or just with two params, the mean and stddev
Support: [0, 1]
Optional
options: { Optional
mean?: numberThe mean of the underlying normal distribution.
Optional
stddev?: numberThe standard deviation of the underlying normal distribution.
A value from the Log-Normal distribution.
NumberValidationError If the input parameters are not valid.
Generates a Gaussian normal value via Box–Muller transform
There are two ways of calling, either with an object with mean and stddev as keys, or just with two params, the mean and stddev
Support: [0, 1]
Optional
mean: numberThe mean of the underlying normal distribution.
Optional
stddev: numberThe standard deviation of the underlying normal distribution.
A value from the Log-Normal distribution.
NumberValidationError If the input parameters are not valid.
Generates a gaussian normal number, but with a special skewing procedure that is sometimes useful.
Optional
options: { Optional
mean?: numberOptional
skew?: numberOptional
stddev?: numberMust be > 0
A normally distributed number
NumberValidationError If the input parameters are not valid.
The chancy function has a very flexible calling pattern.
You can pass it a dice string, an object or a number.
The purpose of this is to have easily serialised random signatures that you can pass to a single function easily.
All chancy distribution functions (that is, when called with ChancyInterface) can be called with min and max parameters, however, it's highly advised to tune your parameters someway else.
Basically, the function will just keep resampling until a figure inside the range is generated. This can quickly lead to large recursion depths for out of bounds inputs, at which point an error is thrown.
Chancy type input
A randomly generated number or an element from a passed array
rng.chancy({ min: 10 }); // Equivalent to calling rng.random(10, Number.MAX_SAFE_INTEGER)
rng.chancy({ max: 10 }); // Equivalent to calling rng.random(0, 10)
rng.chancy({ min: 0, max: 1 }); // Equivalent to calling rng.random(0, 1)
rng.chancy({ type: 'integer', min: 10 }); // Equivalent to calling rng.randInt(10, Number.MAX_SAFE_INTEGER)
rng.chancy({ type: 'integer', max: 10 }); // Equivalent to calling rng.randInt(0, 10)
rng.chancy({ type: 'integer', min: 10, max: 20 }); // Equivalent to calling rng.randInt(10, 20)
rng.chancy({ type: 'normal', ...args }); // Equivalent to calling rng.normal(args)
rng.chancy({ type: 'normal_integer', ...args }); // Equivalent to calling Math.floor(rng.normal(args))
// You can call any of the 'distribution' type functions with chancy as well.
rng.chancy({ type: 'boxMuller', ...args }); // Equivalent to calling rng.boxMuller(args)
rng.chancy({ type: 'bates', ...args }); // Equivalent to calling rng.bates(args)
rng.chancy({ type: 'exponential', ...args }); // Equivalent to calling rng.exponential(args)
This is your monster file monster.json
:
{
"id": "monster",
"hp": {"min": 1, "max": 6, "type": "integer"},
"attack": "1d4"
}
How about a stronger monster, with normally distributed health strong_monster.json
:
{
"id": "strong_monster",
"hp": {"min": 10, "max": 20, "type": "normal_integer"},
"attack": "1d6+1"
}
Or something like this for boss_monster.json
which has a fixed HP:
{
"id": "boss_monster",
"hp": 140,
"attack": "2d10+4"
}
Then in your code:
import {Rng, Chancy} from GameRng;
const rng = new Rng();
class Monster {
hp = 10;
id;
attack = '1d4';
constructor ({id, hp = 10, attack} : {id: string, hp: Chancy, attack: Chancy} = {}) {
this.id = options.id;
this.hp = rng.chancy(hp);
if (attack) this.attack = attack;
}
attack () {
return rng.chancy(this.attack);
}
}
const spec = await fetch('strong_monster.json').then(a => a.json());
const monster = new Monster(spec);
Rounds the results of a chancy call so that it's always an integer.
Not quite equivalent to Math.round(rng.chancy(input)) because it will also transform {type: 'random'} to {type: 'integer'} which aren't quite the same.
'random' has a range of [min, max) whereas interger is [min, max] (inclusive of max).
Given a string dice representation, roll it
e.g. 1d6+5
A random roll on the dice
Given an object representation of a dice, roll it
{n, d, plus} format of dice roll
A random roll on the dice
Roll "n" x "d" sided dice and add "plus"
[n, d, plus] format of dice roll
A random roll on the dice
Roll "n" x "d" sided dice and add "plus"
The number of dice to roll
Optional
d: numberThe number of faces on the dice
Optional
plus: numberThe number to add at the end
A random roll on the dice
Rolls dice and returns the results in an expanded format.
Generates a normally distributed number, but with a special clamping and skewing procedure that is sometimes useful.
Note that the results of this aren't strictly gaussian normal when min/max are present, but for our puposes they should suffice.
Otherwise, without min and max and skew, the results are gaussian normal.
Optional
options: { Optional
max?: numberMaximum value allowed for the output
Optional
mean?: numberThe mean value of the distribution
Optional
min?: numberMinimum value allowed for the output
Optional
skew?: numberThe skew to apply. -ve = left, +ve = right
Optional
stddev?: numberMust be > 0 if present
Optional
depth: numberused internally to track the recursion depth
A normally distributed number
NumberValidationError If the input parameters are not valid.
A random number from [0, 1)
Optional
from: numberLower bound, inclusive
A random number from [from, from+1)
Note that from and to should be interchangeable.
Optional
from: numberLower bound, inclusive
Optional
to: numberUpper bound, exclusive
A random number from [from, to)
Note that from and to should be interchangeable.
Lower bound, inclusive
Upper bound, exclusive
A number by which the numbers should skew. Negative skews towards from, and positive towards to.
A random number from [from, to) skewed a bit skew direction
A random integer from [0, 1]
Note that from and to should be interchangeable.
Optional
from: numberLower bound, inclusive
Optional
to: numberUpper bound, inclusive
A random integer from [from, to]
Note that from and to should be interchangeable.
Optional
from: numberLower bound, inclusive
Optional
to: numberUpper bound, inclusive
Optional
skew: numberA number by which the numbers should skew. Negative skews towards from, and positive towards to.
A random integer from [from, to] skewed a bit in skew direction
Determines the maximum value a Chancy input can take.
Chancy input
Maximum value a call to chancy with these args can take
Determines the minimum value a Chancy input can take.
Chancy input
Minimum value a call to chancy with these args can take
Gives the maximum result of a call to dice with these arguments
Optional
d: numberOptional
plus: numberGives the minimum result of a call to dice with these arguments
Optional
d: numberOptional
plus: numberCreate a new instance. Will use a globally set seed, so every instance returnd by this should generate the same numbers.
An Rng instance, set to the given seed
Create a new instance with the given seed
An Rng instance, set to the given seed
Pass a function that will return uniform random numbers as the source of this rng's randomness.
Supersedes and seed setting.
Optional
func: RandfuncThe random function
Seed the random number generator with the given seed.
Can be a string or a number
Gets the bin "val" sits in when between "min" and "max" is separated by "bins" number of bins
This is right aligning, so .5 rounds up
This is useful when wanting only a discrete number values between two endpoints
The value to bin
The number of bins
Minimum value
Maximum value
The corresponding bin (left aligned)
Parses a string representation of a dice and gives the object representation {n, d, plus}
String dice representation, e.g. '1d6'
The dice representation object
Scales a number from [min, max] to [from, to].
Some might call this linear interpolation.
Min and max default to 0 and 1 respectively
The number - must be 0 <= number <= 1
The min number to scale to. When number === min, will return from
The max number to scale to. When number === max, will return to
Optional
min: numberThe minimum number can take, default 0
Optional
max: numberThe maximum number can take, default 1
A number scaled to the interval [from, to]
Scales a number from [0, 1] to [from, to].
Some might call this linear interpolation
The number - must be 0 <= number <= 1
The min number to scale to. When number === 0, will return from
The max number to scale to. When number === 1, will return to
A normal number scaled to the interval [from, to]
Basic interface required for Rng implementations.
Use this as an interface if you don't need all the advanced distributions