-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
63 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 }, | ||
|
@@ -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}?`, | ||
|