1.0.6 • Published 4 years ago

@amnesic0blex/dice v1.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

dice

The @amnesic0blex/dice module is a collection of simple functions that help with random generation.

Usage

First make sure the package is installed

npm install @amnesic0blex/dice

Table of contents

Dice Roller

Simulates the rolling of an arbitrary number of arbitrary sided dice. In this example @amnesic0blex/dice simulates rolling 1d100 and 4d6. Also the dice.getSum is a reduction function that sums an array. This can be used to get the sum of multiple dice rolls.

const dice = require('@amnesic0blex/dice')
 
console.log(`1d100: ${dice.roll(1,100)}`)
var rolls = dice.roll(4,6)
console.log(`4d6: ${rolls}`)
console.log(`Sum 4d6: ${rolls.reduce(dice.getSum)}`)

Random Integer Generator

Generates a random integer between two values. If only one value is given it is assumed to be the maximum and the minimum is assumed to be 1.

const dice = require('@amnesic0blex/dice')

console.log(dice.randInt(1,10))
console.log(dice.randInt(10))
console.log(dice.randInt(0,10))
console.log(dice.randInt(-5,5))

Coin Flipper

Simulates the flipping of a double-sided coin. Returns boolean true or false.

const dice = require('@amnesic0blex/dice')
var heads = 0
var tails = 0
for (var i = 0; i < 100; ++i) {
    if (dice.flip()) {
        ++heads
    } else {
        ++tails
    }
}
console.log(`Heads: ${heads}`)
console.log(`Tails: ${tails}`)

Ability Score Modifiers

Calculates the 5e ability score modifier from an ability score.

const dice = require('@amnesic0blex/dice')

for (var i = 3; i < 20; ++i) {
    console.log(`Score: ${i}`)
    console.log(`Mod: ${dice.mod(i)}`)
    console.log()
}

Table

Randomly selects a value from a table, with each value weighted evenly.

const dice = require('@amnesic0blex/dice')
var classTable = [
    "Barbarian",
    "Bard",
    "Cleric",
    "Druid",
    "Fighter",
    "Monk",
    "Paladin",
    "Ranger",
    "Rogue",
    "Sorcerer",
    "Warlock",
    "Wizard"
]

console.log(dice.rollTable(classTable))

Weighted Table

Randomly selects a value from a table, with each value weighted individually.

const dice = require('@amnesic0blex/dice')
var raceTable = [
    {
        weight: 1,
        value: "Dragonborn"
    },
    {
        weight: 20,
        value: "Dwarf"
    },
    {
        weight: 15,
        value: "Elf"
    },
    {
        weight: 5,
        value: "Gnome"
    },
    {
        weight: 10,
        value: "Half-Elf"
    },
    {
        weight: 7,
        value: "Half-Orc"
    },
    {
        weight: 5,
        value: "Halfling"
    },
    {
        weight: 35,
        value: "Human"
    },
    {
        weight: 2,
        value: "Tiefling"
    }
]

console.log(dice.rollWeightedTable(raceTable))
1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago