SHIFT + D

determine

Updated on August 22, 2024Source codeTests

createDetermine is a pipe that transforms a number (usually generated randomly) to some outcome (which can be any data type) based on the probability of each outcome.

Create determine

Call createDetermine with these parameters to create your determine function:

Parameter
Type
Required
Description
potentialities
Array
yes

An array of object, each of which specify an outcome (any data type) and a probability (number).

The probability numbers don't have to be on any particular scale. They're treated as weighted values, relative to the sum of all probability values.

For example, if you pass four objects, each with a probability of 0.25, and then call determine(Math.random()), each outcome will have a 25% chance of occurring

Likewise, if you pass four objects, each with a probability of 1, and then call determine(Math.random()), each outcome will still have a 25% chance of occurring, because the probability values of 1 are weighted relative to the probability sum of 4.

Example

import { createDetermine } from '@baleada/logic'

const determine = createDetermine([
  { outcome: 'red', probability: 1 },
  { outcome: 'green', probability: 1 },
  { outcome: 'blue', probability: 1 },
])

// Each outcome has a 33% chance of occurring
const outcome = determine(Math.random());
import { createDetermine } from '@baleada/logic'

const determine = createDetermine([
  { outcome: 'red', probability: 3 },
  { outcome: 'green', probability: 0 },
  { outcome: 'blue', probability: 0 },
])

// `red` has a 100% chance of occurring
const outcome = determine(Math.random());
import { createDetermine } from '@baleada/logic'
const determine = createDetermine([
  { outcome: 'red', probability: 2 },
  { outcome: 'green', probability: 1 },
  { outcome: 'blue', probability: 1 },
])

// `red` has a 50% chance of occurring, and both
// `green` and `blue` have a 25% chance of occurring
const outcome = determine(Math.random());

deep mergeentries

Edit doc on GitHub

ON THIS PAGE

determineCreate determineExample