Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bratwurm-probabilities

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bratwurm-probabilities - npm Package Compare versions

Comparing version 1.0.1 to 1.0.2

64

bratwurm/bratwurmFunction.spec.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const bratwurmFunctions_1 = require("./bratwurmFunctions");
const math = require("mathjs");
const mathjs_1 = require("mathjs");
function getState(diceCount, fehlwurf = false) {

@@ -14,9 +14,9 @@ return {

it('should compute correct prob for 1 missing dice and no bratwurm', () => {
expect(bratwurmFunctions_1.probabilityOfFehlwurf(getState([7, 0, 0, 0, 0, 0]))).toBeCloseTo(5 / 6);
expect((0, bratwurmFunctions_1.probabilityOfFehlwurf)(getState([7, 0, 0, 0, 0, 0]))).toBeCloseTo(5 / 6);
});
it('should compute correct prob for 1 missing dice and existing bratwurm', () => {
expect(bratwurmFunctions_1.probabilityOfFehlwurf(getState([0, 0, 0, 0, 0, 7]))).toBeCloseTo(1 / 6);
expect((0, bratwurmFunctions_1.probabilityOfFehlwurf)(getState([0, 0, 0, 0, 0, 7]))).toBeCloseTo(1 / 6);
});
it('should compute correct prob for 1 missing dice and 1 free slot and existing bratwurm', () => {
expect(bratwurmFunctions_1.probabilityOfFehlwurf(getState([0, 1, 1, 1, 1, 3]))).toBeCloseTo(5 / 6);
expect((0, bratwurmFunctions_1.probabilityOfFehlwurf)(getState([0, 1, 1, 1, 1, 3]))).toBeCloseTo(5 / 6);
});

@@ -26,6 +26,6 @@ });

it('should compute correct sum', () => {
expect(bratwurmFunctions_1.getSum([0, 0, 0, 0, 0, 1])).toEqual(5);
expect(bratwurmFunctions_1.getSum([0, 0, 0, 0, 1, 0])).toEqual(5);
expect(bratwurmFunctions_1.getSum([0, 0, 0, 1, 0, 0])).toEqual(4);
expect(bratwurmFunctions_1.getSum([0, 0, 0, 1, 1, 1])).toEqual(14);
expect((0, bratwurmFunctions_1.getSum)([0, 0, 0, 0, 0, 1])).toEqual(5);
expect((0, bratwurmFunctions_1.getSum)([0, 0, 0, 0, 1, 0])).toEqual(5);
expect((0, bratwurmFunctions_1.getSum)([0, 0, 0, 1, 0, 0])).toEqual(4);
expect((0, bratwurmFunctions_1.getSum)([0, 0, 0, 1, 1, 1])).toEqual(14);
});

@@ -35,45 +35,51 @@ });

it('should return 0 for too high sum', () => {
expect(math.string(bratwurmFunctions_1.probExact(21, getState([0, 0, 0, 0, 0, 5])))).toEqual('0');
expect(math.string(bratwurmFunctions_1.probExact(21, getState([0, 0, 0, 0, 5, 0])))).toEqual('0');
expect((0, mathjs_1.string)((0, bratwurmFunctions_1.probExact)(21, getState([0, 0, 0, 0, 0, 5])))).toEqual('0');
expect((0, mathjs_1.string)((0, bratwurmFunctions_1.probExact)(21, getState([0, 0, 0, 0, 5, 0])))).toEqual('0');
});
it('should return 0 for fehlwurf', () => {
expect(math.string(bratwurmFunctions_1.probExact(21, getState([0, 0, 0, 0, 0, 1], true)))).toEqual('0');
expect(math.string(bratwurmFunctions_1.probExact(21, getState([0, 0, 0, 0, 1, 0], true)))).toEqual('0');
expect((0, mathjs_1.string)((0, bratwurmFunctions_1.probExact)(21, getState([0, 0, 0, 0, 0, 1], true)))).toEqual('0');
expect((0, mathjs_1.string)((0, bratwurmFunctions_1.probExact)(21, getState([0, 0, 0, 0, 1, 0], true)))).toEqual('0');
});
it('should return 0 if all dice used', () => {
expect(math.string(bratwurmFunctions_1.probExact(21, getState([7, 0, 0, 0, 0, 1])))).toEqual('0');
expect(math.string(bratwurmFunctions_1.probExact(21, getState([7, 0, 0, 0, 1, 0])))).toEqual('0');
expect((0, mathjs_1.string)((0, bratwurmFunctions_1.probExact)(21, getState([7, 0, 0, 0, 0, 1])))).toEqual('0');
expect((0, mathjs_1.string)((0, bratwurmFunctions_1.probExact)(21, getState([7, 0, 0, 0, 1, 0])))).toEqual('0');
});
it('should return 1 if sum matches', () => {
expect(math.string(bratwurmFunctions_1.probExact(21, getState([1, 0, 0, 0, 0, 4])))).toEqual('1');
expect(math.string(bratwurmFunctions_1.probExact(21, getState([1, 0, 0, 0, 4, 0])))).toEqual('1');
it('should return 1 if sum matches and contains bratwurm', () => {
expect((0, mathjs_1.string)((0, bratwurmFunctions_1.probExact)(21, getState([1, 0, 0, 0, 0, 4])))).toEqual('1');
});
it('should return 0 if sum matches but does not contain bratwurm', () => {
expect((0, mathjs_1.string)((0, bratwurmFunctions_1.probExact)(21, getState([1, 0, 0, 0, 4, 0])))).toEqual('0');
});
// https://www.omnicalculator.com/statistics/dice?v=dice_type:6,target_value2:0,target_value3:0,number_of_dice:3,game_option:4.000000000000000,target_value1:1,target_dice:1
it('should return correct simple recursive value', () => {
expect(bratwurmFunctions_1.probExact(21, getState([0, 0, 0, 0, 0, 4]))).toBeCloseTo(0.3858);
expect((0, bratwurmFunctions_1.probExact)(21, getState([0, 0, 0, 0, 0, 4]))).toBeCloseTo(0.3858025);
});
it('should return correct recursive value', () => {
console.debug = () => null;
expect(bratwurmFunctions_1.probExact(21, getState([0, 0, 0, 0, 0, 0]))).toBeCloseTo(0.4951502336);
expect((0, bratwurmFunctions_1.probExact)(21, getState([0, 0, 0, 0, 0, 0]))).toBeCloseTo(0.439596789623015);
});
});
describe('probAtLeast', () => {
it('should return 1 for too high sum', () => {
expect(math.string(bratwurmFunctions_1.probAtLeast(21, getState([0, 0, 0, 0, 0, 5])))).toEqual('1');
expect(math.string(bratwurmFunctions_1.probAtLeast(21, getState([0, 0, 0, 0, 5, 0])))).toEqual('1');
it('should return 1 for too high sum if bratwurm is contained', () => {
expect((0, mathjs_1.string)((0, bratwurmFunctions_1.probAtLeast)(21, getState([0, 0, 0, 0, 0, 5])))).toEqual('1');
});
it('should return < 1 for too high sum if bratwurm is not contained', () => {
expect((0, bratwurmFunctions_1.probAtLeast)(21, getState([0, 0, 0, 0, 7, 0]))).toBeCloseTo(1 / 6);
});
it('should return 0 if all dice used', () => {
expect(math.string(bratwurmFunctions_1.probAtLeast(21, getState([7, 0, 0, 0, 0, 1])))).toEqual('0');
expect(math.string(bratwurmFunctions_1.probAtLeast(21, getState([7, 0, 0, 0, 1, 0])))).toEqual('0');
expect((0, mathjs_1.string)((0, bratwurmFunctions_1.probAtLeast)(21, getState([7, 0, 0, 0, 0, 1])))).toEqual('0');
expect((0, mathjs_1.string)((0, bratwurmFunctions_1.probAtLeast)(21, getState([7, 0, 0, 0, 1, 0])))).toEqual('0');
});
it('should return 1 if sum matches', () => {
expect(math.string(bratwurmFunctions_1.probAtLeast(21, getState([1, 0, 0, 0, 0, 4])))).toEqual('1');
expect(math.string(bratwurmFunctions_1.probAtLeast(21, getState([1, 0, 0, 0, 4, 0])))).toEqual('1');
it('should return 1 if sum matches and bratwurm contained', () => {
expect((0, mathjs_1.string)((0, bratwurmFunctions_1.probAtLeast)(21, getState([1, 0, 0, 0, 0, 4])))).toEqual('1');
});
it('should return < 1 if sum matches and bratwurm not contained', () => {
expect((0, bratwurmFunctions_1.probAtLeast)(21, getState([1, 0, 0, 0, 4, 0]))).toBeLessThan(1);
});
it('should return correct simple recursive value', () => {
expect(bratwurmFunctions_1.probAtLeast(21, getState([0, 0, 0, 0, 0, 4]))).toBeCloseTo(0.9992);
expect((0, bratwurmFunctions_1.probAtLeast)(21, getState([0, 0, 0, 0, 0, 4]))).toBeCloseTo(0.9992);
});
it('should return correct recursive value', () => {
console.debug = () => null;
expect(bratwurmFunctions_1.probAtLeast(21, getState([0, 0, 0, 0, 0, 0]))).toBeCloseTo(0.9380313106);
expect((0, bratwurmFunctions_1.probAtLeast)(21, getState([0, 0, 0, 0, 0, 0]))).toBeCloseTo(0.893026737150746);
});

@@ -80,0 +86,0 @@ });

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.computeProbs = exports.prob = exports.probExact = exports.probAtLeast = exports.getSum = exports.situations = exports.probabilityOfFehlwurf = exports.TARGETS = exports.TOTAL_DICES = void 0;
const bratwurmState_1 = require("./bratwurmState");

@@ -9,8 +10,8 @@ const mathjs_1 = require("mathjs");

function probabilityOfFehlwurf(state) {
const stateWithProb100 = Object.assign(Object.assign({}, state), { thrown: Object.assign(Object.assign({}, state.thrown), { probability: mathjs_1.fraction(1) }) });
let fehlwurfProb = mathjs_1.fraction(0);
const stateWithProb100 = Object.assign(Object.assign({}, state), { thrown: Object.assign(Object.assign({}, state.thrown), { probability: (0, mathjs_1.fraction)(1) }) });
let fehlwurfProb = (0, mathjs_1.fraction)(0);
for (const nextState of exampleNextStateGen(stateWithProb100)) {
// console.debug('next state: ', nextState);
if (nextState.fehlWurf) {
fehlwurfProb = mathjs_1.add(fehlwurfProb, nextState.thrown.probability);
fehlwurfProb = (0, mathjs_1.add)(fehlwurfProb, nextState.thrown.probability);
}

@@ -22,3 +23,3 @@ }

function* getNextStateForThrow(state, wurf) {
const remainingDice = mathjs_1.sum(...wurf.diceCount);
const remainingDice = (0, mathjs_1.sum)(...wurf.diceCount);
let fehlWurf = true;

@@ -35,3 +36,3 @@ for (let d = 0; d < throw_1.DICE_FACES; d++) {

diceCount: newThrown,
probability: mathjs_1.multiply(state.thrown.probability, wurf.probability)
probability: (0, mathjs_1.multiply)(state.thrown.probability, wurf.probability)
} });

@@ -41,7 +42,7 @@ }

if (fehlWurf) {
yield Object.assign(Object.assign({}, state), { thrown: Object.assign(Object.assign({}, state.thrown), { probability: mathjs_1.multiply(state.thrown.probability, wurf.probability) }), fehlWurf: true });
yield Object.assign(Object.assign({}, state), { thrown: Object.assign(Object.assign({}, state.thrown), { probability: (0, mathjs_1.multiply)(state.thrown.probability, wurf.probability) }), fehlWurf: true });
}
}
function* exampleNextStateGen(state) {
let alreadyThrown = mathjs_1.sum(state.thrown.diceCount);
let alreadyThrown = (0, mathjs_1.sum)(state.thrown.diceCount);
if (state.fehlWurf || alreadyThrown === exports.TOTAL_DICES) {

@@ -52,3 +53,3 @@ return;

// console.debug('remainingDice: ', remainingDice);
for (const wurf of throw_1.getAllThrows(remainingDice)) {
for (const wurf of (0, throw_1.getAllThrows)(remainingDice)) {
// console.debug('wurf: ', wurf);

@@ -62,3 +63,3 @@ for (const next of getNextStateForThrow(state, wurf)) {

for (let diceCount = 0; diceCount < exports.TOTAL_DICES; diceCount++) {
for (const t of throw_1.getAllThrows(diceCount)) {
for (const t of (0, throw_1.getAllThrows)(diceCount)) {
yield t;

@@ -76,7 +77,7 @@ }

const currentSum = getSum(finalState.thrown.diceCount);
const diceUsed = mathjs_1.sum(...finalState.thrown.diceCount);
const diceUsed = (0, mathjs_1.sum)(...finalState.thrown.diceCount);
if (finalState.fehlWurf || (diceUsed === exports.TOTAL_DICES && currentSum < target)) {
return 0;
}
if (currentSum >= target) {
if (currentSum >= target && finalState.thrown.diceCount[5] > 0) {
return 1;

@@ -91,7 +92,7 @@ }

const currentSum = getSum(finalState.thrown.diceCount);
const diceUsed = mathjs_1.sum(...finalState.thrown.diceCount);
const diceUsed = (0, mathjs_1.sum)(...finalState.thrown.diceCount);
if (finalState.fehlWurf || currentSum > target) {
return 0;
}
if (currentSum === target) {
if (currentSum === target && finalState.thrown.diceCount[5] > 0) {
return 1;

@@ -116,3 +117,3 @@ }

}
const stateKey = bratwurmState_1.serializeBratwurmState(state);
const stateKey = (0, bratwurmState_1.serializeBratwurmState)(state);
if (!cache.has(stateKey)) {

@@ -124,7 +125,7 @@ // console.debug('computing for state: ', stateKey);

}
const remainingDice = exports.TOTAL_DICES - mathjs_1.sum(...state.thrown.diceCount);
let probTotal = mathjs_1.fraction(0);
for (const wurf of throw_1.getAllThrows(remainingDice)) {
const maxProbability = mathjs_1.max(...([...getNextStateForThrow(state, wurf)].map(s => prob(target, s, terminate, cache))));
probTotal = mathjs_1.sum(probTotal, mathjs_1.multiply(wurf.probability, maxProbability));
const remainingDice = exports.TOTAL_DICES - (0, mathjs_1.sum)(...state.thrown.diceCount);
let probTotal = (0, mathjs_1.fraction)(0);
for (const wurf of (0, throw_1.getAllThrows)(remainingDice)) {
const maxProbability = (0, mathjs_1.max)(...([...getNextStateForThrow(state, wurf)].map(s => prob(target, s, terminate, cache))));
probTotal = (0, mathjs_1.sum)(probTotal, (0, mathjs_1.multiply)(wurf.probability, maxProbability));
}

@@ -146,4 +147,4 @@ cache.set(stateKey, probTotal);

}, caches.get(target)));
if (mathjs_1.larger(mathjs_1.sum(...probs), 0)) { // only non-trivial results
yield { diceCounts: t.diceCount, probs: probs.map(p => mathjs_1.number(p)) };
if ((0, mathjs_1.larger)((0, mathjs_1.sum)(...probs), 0)) { // only non-trivial results
yield { diceCounts: t.diceCount, probs: probs.map(p => (0, mathjs_1.number)(p)) };
}

@@ -150,0 +151,0 @@ }

@@ -88,3 +88,3 @@ import {BratwurmState, serializeBratwurmState} from './bratwurmState';

}
if (currentSum >= target) {
if (currentSum >= target && finalState.thrown.diceCount[5] > 0) {
return 1;

@@ -104,3 +104,3 @@ }

}
if (currentSum === target) {
if (currentSum === target && finalState.thrown.diceCount[5] > 0) {
return 1;

@@ -107,0 +107,0 @@ }

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.serializeBratwurmState = void 0;
function serializeBratwurmState(state) {

@@ -4,0 +5,0 @@ return `fw=${state.fehlWurf},${state.thrown.diceCount.join(',')}`;

@@ -8,3 +8,3 @@ "use strict";

for (let diceCount = 0; diceCount < bratwurmFunctions_1.TOTAL_DICES; diceCount++) {
for (const t of throw_1.getAllThrows(diceCount)) {
for (const t of (0, throw_1.getAllThrows)(diceCount)) {
count++;

@@ -11,0 +11,0 @@ }

@@ -21,3 +21,3 @@ "use strict";

console.debug('existingDice: ', existingDice);
const prob = bratwurmFunctions_1.probabilityOfFehlwurf({
const prob = (0, bratwurmFunctions_1.probabilityOfFehlwurf)({
thrown: { diceCount: existingDice, probability: 1 },

@@ -24,0 +24,0 @@ fehlWurf: false

@@ -11,3 +11,3 @@ "use strict";

const probFunction = process.argv.slice(2).some(arg => arg === 'sum') ? bratwurmFunctions_1.probAtLeast : bratwurmFunctions_1.probExact;
const orderedSituations = [...bratwurmFunctions_1.situations()];
const orderedSituations = [...(0, bratwurmFunctions_1.situations)()];
orderedSituations.sort((a, b) => a.diceCount.join('') < b.diceCount.join('') ? -1 : 1);

@@ -17,3 +17,3 @@ for (const t of orderedSituations) {

// console.log('unused dice: ', unusedDice);
const remaining = bratwurmFunctions_1.TOTAL_DICES - mathjs_1.sum(...t.diceCount);
const remaining = bratwurmFunctions_1.TOTAL_DICES - (0, mathjs_1.sum)(...t.diceCount);
const originalState = {

@@ -24,3 +24,3 @@ thrown: t,

const probs = bratwurmFunctions_1.TARGETS.map(target => probFunction(target, originalState, caches.get(target)));
if (mathjs_1.larger(mathjs_1.sum(...probs), 0) && probs.some(p => mathjs_1.larger(p, 0) && mathjs_1.larger(1, p))) { // only non-trivial results
if ((0, mathjs_1.larger)((0, mathjs_1.sum)(...probs), 0) && probs.some(p => (0, mathjs_1.larger)(p, 0) && (0, mathjs_1.larger)(1, p))) { // only non-trivial results
unusedDice.forEach((unusedDice) => {

@@ -35,3 +35,3 @@ for (let diceCount = 1; diceCount <= remaining; diceCount++) {

const probs = bratwurmFunctions_1.TARGETS.map(target => probFunction(target, state, caches.get(target)));
if (mathjs_1.larger(mathjs_1.sum(...probs), 0) && probs.some(p => mathjs_1.larger(p, 0) && mathjs_1.larger(1, p))) { // only non-trivial results
if ((0, mathjs_1.larger)((0, mathjs_1.sum)(...probs), 0) && probs.some(p => (0, mathjs_1.larger)(p, 0) && (0, mathjs_1.larger)(1, p))) { // only non-trivial results
// console.log('probs: ', probs);

@@ -38,0 +38,0 @@ console.log(`situation=${t.diceCount.join(',')} (${diceCount}x${unusedDice + 1}): ${bratwurmFunctions_1.TARGETS.map((target, i) => `${target}=${math.number(probs[i])}`).join(',')}`);

@@ -7,5 +7,5 @@ "use strict";

const sumF = process.argv.slice(2).some(arg => arg === 'sum');
for (const x of bratwurmFunctions_1.computeProbs(sumF)) {
for (const x of (0, bratwurmFunctions_1.computeProbs)(sumF)) {
console.log(`situation=${x.diceCounts.join(',')}: ${bratwurmFunctions_1.TARGETS.map((target, i) => `${target}=${x.probs[i]}`).join(',')}`);
}
//# sourceMappingURL=probabilityForTargetTable.js.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAllThrows = exports.probabilityOf = exports.getNextThrow = exports.DICE_FACES = void 0;
const mathjs_1 = require("mathjs");

@@ -19,5 +20,5 @@ exports.DICE_FACES = 6;

function probabilityOf(diceCount) {
const total = mathjs_1.sum(...diceCount);
const total = (0, mathjs_1.sum)(...diceCount);
if (total <= 0) {
return mathjs_1.fraction(0);
return (0, mathjs_1.fraction)(0);
}

@@ -29,6 +30,6 @@ let remainingDiceCount = total;

diceCount.forEach(count => {
possibilities *= mathjs_1.combinations(remainingDiceCount, count);
possibilities *= (0, mathjs_1.combinations)(remainingDiceCount, count);
remainingDiceCount -= count;
});
return mathjs_1.fraction(possibilities, mathjs_1.pow(exports.DICE_FACES, total));
return (0, mathjs_1.fraction)(possibilities, (0, mathjs_1.pow)(exports.DICE_FACES, total));
}

@@ -35,0 +36,0 @@ exports.probabilityOf = probabilityOf;

@@ -8,4 +8,4 @@ "use strict";

it('should compute correct probs for 1 dice', () => {
expect(throw_1.probabilityOf([1, 1])).toBeCloseTo(1 / 18);
expect(throw_1.probabilityOf([2, 0])).toBeCloseTo(1 / 36);
expect((0, throw_1.probabilityOf)([1, 1])).toBeCloseTo(1 / 18);
expect((0, throw_1.probabilityOf)([2, 0])).toBeCloseTo(1 / 36);
});

@@ -15,3 +15,3 @@ });

it('should return correct value for 0 dice count', () => {
expect([...throw_1.getAllThrows(0)]).toEqual([{ diceCount: [0, 0, 0, 0, 0, 0], probability: mathjs_1.fraction(0) }]);
expect([...(0, throw_1.getAllThrows)(0)]).toEqual([{ diceCount: [0, 0, 0, 0, 0, 0], probability: (0, mathjs_1.fraction)(0) }]);
});

@@ -18,0 +18,0 @@ });

{
"name": "bratwurm-probabilities",
"version": "1.0.1",
"version": "1.0.2",
"description": "",

@@ -18,7 +18,7 @@ "main": "index.js",

"devDependencies": {
"@types/jasmine": "^3.5.0",
"@types/jasmine": "^4.3.1",
"@types/node": "^13.1.2",
"jasmine": "^3.5.0",
"typescript": "~3.7.4"
"jasmine": "^4.5.0",
"typescript": "~4.9.4"
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc