Skip to content

Instantly share code, notes, and snippets.

@dsetzer
dsetzer / prob_using_coefficients.py
Last active August 17, 2023 16:16
Calculating probabilities with coefficients calculated from simulations
import hmac
import hashlib
import binascii
import math
from collections import Counter
from scipy.optimize import curve_fit
import numpy as np
# Number of games to simulate (10 million)
num_games_to_simulate = 10_000_000
@dsetzer
dsetzer / run-length-compression.js
Created May 24, 2023 02:50
RunLength Encoding (RLE) text compression using Unicode symbols
/**
* Run-Length Encoding (RLE) text compression using Unicode symbols.
*
* This algorithm compresses text by counting the number of consecutive identical
* characters in the input string and representing them in a compact format within
* the compressed string using Unicode characters. The encoded and decoded parts
* are separated by a Unicode private-use character, U+FFF0.
*
* @param {string} inputText - The text to be compressed.
* @returns {string} - The compressed text.
@dsetzer
dsetzer / mapRange.js
Created April 29, 2023 05:43
Map a value from one range to another range
/**
* Map a value from one range to another
* @param {number} value - The value to map
* @param {number} inMin - The minimum value of the input range
* @param {number} inMax - The maximum value of the input range
* @param {number} outMin - The minimum value of the output range
* @param {number} outMax - The maximum value of the output range
* @returns {number} The mapped value
*/
const mapRange = (value, inMin, inMax, outMin, outMax) => {
@dsetzer
dsetzer / README.md
Created January 26, 2023 20:50 — forked from fahadysf/README.md
A multiprocess task broker which accepts and provides status reports for tasks using JSON REST API calls.

Multiprocess Task Broker with REST API

This gist shows and example or an asynchronous multiprocess task broker which can take job requests and report on running jobs via a minimal REST API.

Adapted from https://gist.github.com/nitaku/10d0662536f37a087e1b

All of the caveats from the original author still apply.

@dsetzer
dsetzer / cumulativeProbs.js
Created July 29, 2022 13:02
Probability functions (codex generated)
// Cumulative Geometric Probability
function getGeoProb(p, n) {
return (1 - Math.pow(p, n)) / (1 - p);
}
// Cumulative Hypergeometric Probability
function getHyperProb(N, n, K, k) {
return getCombin(K, k) * getCombin(N - K, n - k) / getCombin(N, n);
}
@dsetzer
dsetzer / StreakSegment.js
Last active June 18, 2023 16:14
Function to split up an array into separate groups of W/L streaks.
/**
* Takes in an array of numbers and returns an array of streaks of numbers.
* @param {Array} results - An array of numbers.
* @param {Number} multiplier - The number that determines the streak.
* @returns {Array} - An array of streaks of numbers.
*/
function getStreaks(results, multiplier) {
let streaks = new Array();
let streak = new Array();
for (let i = 0; i < results.length; i++) {
@dsetzer
dsetzer / ResolveKeyWorkers.js
Last active July 25, 2022 20:10
Partial Private Key Solver
async function resolveKeyWorkers(brokenKey, startIter, numWorkers) {
var updateFrequency = 100000 * Number(numWorkers);
var splitKey = brokenKey.split("?");
var iteratorStart = Number(startIter) || Math.pow(58, (splitKey.length - 2));
var duration = Math.pow(58, (splitKey.length - 1));
var workers = [];
var latestStatus = null, latestUpdate = 0;
var statusUpdate = function (info) {
if (!latestStatus || info[0] > latestStatus[0]) {
latestStatus = info;
@dsetzer
dsetzer / IterativeProb.js
Created October 25, 2021 13:17
Finds the cumulative win probability of a target payout in bustabit/bustadice. Results parameter expects an ordered array of numbers representing the games bust history.
/**
* Calculates current cumulative geometric probability
* @param payout Target payout to calculate for
* @param results Array of ordered results
* @param precision Num decimal places accuracy
* @param debug Output result slice to console
* @returns Cumulative geometric win probability
*/
function getCumulativeProb(payout, results, precision, debug = false) {
let prc = 10 ** Math.max(1, Math.min((precision || 18), 18), 1);
@dsetzer
dsetzer / Pattern-System-v1.4.js
Last active December 19, 2022 11:14
Dynamic Pattern System v1.4 work in progress prototype for bustabit/bustadice scripts.
// ----------------------- V1.4 -------------------------
// This utility function will check an array of game results for a given pattern and return the most recent occurrence of a
// match. The pattern is specified using a regex-style syntax that uses the available tokens defined in the token list. The
// token list is an array of objects containing a token letter and a callback function that is used to check if the game results
// match the specified condition. It returns an array containing the game result objects for the matched results.
function checkPattern(pattern, results, tokens){
let tokenLetters = pattern.match(/\w/g);
let filteredTokens = tokens.filter((token) => tokenLetters.includes(token[0]));
let res = results.map((r, i) => (`[${i}${filteredTokens.filter((e) => e[1](r)).map((e) => (e[0])).join('')}]`)).reverse().join('');
@dsetzer
dsetzer / downloadString.js
Last active August 31, 2021 00:18
Initiates a file download from javascript. No longer works from within sandboxed iframes.
function downloadString(data, fileName = 'download.txt', fileType = 'text/plain') {
var blob = new Blob([data], {type: fileType});
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(blob, fileName);
} else{
var e = document.createEvent('MouseEvents'),
a = document.createElement('a');
a.download = fileName;
a.href = window.URL.createObjectURL(blob);
a.dataset.downloadurl = [fileType, a.download, a.href].join(':');