diff --git a/README.md b/README.md index 86f25e2..93f223f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # Fortuna +The quick solution for everything random! A Gacha-like system to roll random items with weights. diff --git a/deno.json b/deno.json index 5ddef40..3820d9e 100644 --- a/deno.json +++ b/deno.json @@ -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" + ] + } } } \ No newline at end of file diff --git a/deno.lock b/deno.lock index b8f45bc..f5be2f9 100644 --- a/deno.lock +++ b/deno.lock @@ -5,6 +5,13 @@ "https://deno.land/std@0.190.0/testing/_diff.ts": "1a3c044aedf77647d6cac86b798c6417603361b66b54c53331b312caeb447aea", "https://deno.land/std@0.190.0/testing/_format.ts": "a69126e8a469009adf4cf2a50af889aca364c349797e63174884a52ff75cf4c7", "https://deno.land/std@0.190.0/testing/asserts.ts": "e16d98b4d73ffc4ed498d717307a12500ae4f2cbe668f1a215632d19fcffc22f", + "https://deno.land/x/fortuna@v4.0.0/benches/comparison.ts": "4240603a03c69dc0316ca92f019e8f17ded9ef8fbd9f53ac1a9a11c66dbadae0", + "https://deno.land/x/fortuna@v4.0.0/mod.ts": "f9a42cb7bb879d2ce1f1e0bf52716c620ee71a9f67769bef8887cbf8d9a918f0", + "https://deno.land/x/fortuna@v4.0.0/src/die.ts": "20261238db73c895204408e7d030b6650f0df03d2c77f522cb6bfbe79701ec9c", + "https://deno.land/x/fortuna@v4.0.0/src/limited_machine.ts": "d4103726625ac387fa6499a202324985f59e8dc6f52f18c91a51515d4709c9b6", + "https://deno.land/x/fortuna@v4.0.0/src/machine.ts": "1772a67f974120238e03d99bf0e924a24c88ba9d9e13b81f40c6f9327774da65", + "https://deno.land/x/fortuna@v4.0.0/src/roll.ts": "dbe1eac702bb3e2fd152397c74cc1e4dfbde2dc02a331c12dc192da5739d0387", + "https://deno.land/x/fortuna@v4.0.0/testdata/pokemon.json": "6dac3378ac42eab9175145915cab85e5a959fa044686f43459d2da205625f386", "https://deno.land/x/masx200_weighted_randomly_select@2.1.1/mod.ts": "5d7fee96350e0df360363b8b5b3c784251248e6dfcc355c7e0639cb20a1e895b", "https://deno.land/x/masx200_weighted_randomly_select@2.1.1/src/index.ts": "7441a8a37df57a178fd1dd6b08de9eb192899a4068622b63b50995e1e7a89ad5", "https://deno.land/x/wrand@v1.2.0/index.ts": "5c9977efa424a8efffc1e0d29b8d9ef81a6009c0362c303d87030b320aa58a55", diff --git a/dist/coin.js b/dist/coin.js new file mode 100644 index 0000000..d84b75b --- /dev/null +++ b/dist/coin.js @@ -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 }; diff --git a/mod.ts b/mod.ts index 96bebdf..f0a9950 100644 --- a/mod.ts +++ b/mod.ts @@ -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" \ No newline at end of file diff --git a/src/coin.ts b/src/coin.ts new file mode 100644 index 0000000..37a15ca --- /dev/null +++ b/src/coin.ts @@ -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") +} \ No newline at end of file diff --git a/src/die.ts b/src/die.ts index 87638bc..f7bdc74 100644 --- a/src/die.ts +++ b/src/die.ts @@ -1,3 +1,5 @@ +// A biased die can be implemented using GachaMachine. + export interface DieConfig { times: number; face: number; diff --git a/test.ts b/test.ts index 9200bc8..bf46abe 100644 --- a/test.ts +++ b/test.ts @@ -6,6 +6,7 @@ import { assertThrows, } from "https://deno.land/std@0.190.0/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 }, @@ -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}?`,