Skip to content

Commit

Permalink
add unbiased coin
Browse files Browse the repository at this point in the history
  • Loading branch information
retraigo committed Jul 20, 2023
1 parent 94eccd0 commit bc359a4
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Fortuna
The quick solution for everything random!

A Gacha-like system to roll random items with weights.

Expand Down
16 changes: 13 additions & 3 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
{
"tasks": {
"bench": "deno bench --unstable benches/comparison.ts",
"test": "deno test test.ts"
"bench": {
"files": {
"include": [
"./benches/comparison.ts"
]
}
},
"test": {
"files": {
"include": [
"./test.ts"
]
}
}
}
7 changes: 7 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions dist/coin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// deno-fmt-ignore-file
// deno-lint-ignore-file
// This code was bundled using `deno bundle` and it's not recommended to edit it manually

function unbiasedCoin(n = 1) {
const upperBound = 1 << n;
const res = Math.floor(Math.random() * upperBound).toString(2).padStart(n, "0");
return res.split("").map((x)=>x === "0" ? "H" : "T");
}
export { unbiasedCoin as unbiasedCoin };
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { GachaMachine } from "./src/machine.ts";
export { LimitedGachaMachine } from "./src/limited_machine.ts";
export { rollDie } from "./src/die.ts";
export { roll } from "./src/roll.ts";
export { unbiasedCoin } from "./src/coin.ts"
12 changes: 12 additions & 0 deletions src/coin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// A biased coin can be implemented using GachaMachine.

/**
* Toss n unbiased coins (50% chance of head / tail).
* @param n Number of coins to toss.
* @returns Array of results. Returns "H" for head and "T" for tail.
*/
export function unbiasedCoin(n = 1): ("H" | "T")[] {
const upperBound = 1 << n
const res = Math.floor(Math.random() * upperBound).toString(2).padStart(n, "0")
return res.split("").map(x => x === "0" ? "H" : "T")
}
2 changes: 2 additions & 0 deletions src/die.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// A biased die can be implemented using GachaMachine.

export interface DieConfig {
times: number;
face: number;
Expand Down
17 changes: 17 additions & 0 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
assertThrows,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import { GachaMachine, LimitedGachaMachine, roll, rollDie } from "./mod.ts";
import { unbiasedCoin } from "./src/coin.ts";

const testData = [
{ result: "SSR cool character", chance: 1 },
Expand Down Expand Up @@ -183,6 +184,22 @@ Deno.test({
},
});

Deno.test({
name: `Toss an unbiased coin (should return ["H"] or ["T"])`,
fn() {
const res = unbiasedCoin();
assert(res.length === 1 && res[0] === "H" || res[0] === "T");
},
});

Deno.test({
name: `Toss N unbiased coins (should return an array of "H" or "T")`,
fn() {
const res = unbiasedCoin();
assert(res.length === 1 && res.every((x) => x === "H" || x === "T"));
},
});

/*
Deno.test({
name: `${sortedTestData[0].chance} in ${testData.reduce((acc, val) => acc + val.chance, 0)} rolls return a ${sortedTestData[0].result}?`,
Expand Down

0 comments on commit bc359a4

Please sign in to comment.