-
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
1 parent
4beefba
commit bf8fd70
Showing
6 changed files
with
146 additions
and
1 deletion.
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
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,27 @@ | ||
import { assertEquals } from "https://deno.land/std@0.160.0/testing/asserts.ts"; | ||
|
||
import * as Peach from "../mod.ts"; | ||
|
||
Deno.test({ | ||
name: "Peach.BigInt.Uniform | Choosing 0...0 returns 0", | ||
fn() { | ||
assertEquals(Peach.BigInt.uniform(0n, 0n)(), 0n, "uniform 0...0 must be zero"); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "Peach.BigInt.Uniform | Always in range", | ||
fn() { | ||
const upper = Peach.BigInt.uniform(0n, 10n); | ||
|
||
for (let idx = 0; idx < 1_000; idx++) { | ||
const sizeTgt = upper(); | ||
const random = Peach.BigInt.uniform(0n, sizeTgt); | ||
const val = random(); | ||
|
||
if (val < 0 || val > sizeTgt) { | ||
throw new Error(`out of range 0...${sizeTgt}: ${val}`); | ||
} | ||
} | ||
}, | ||
}); |
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,31 @@ | ||
import type { Thunk, Wrapped } from "../types.ts"; | ||
import { unwrap } from "../types.ts"; | ||
|
||
/** | ||
* Return a random integer in the chosen range. The distribution is | ||
* uniform (pseudo RNG willing). | ||
* | ||
* @param from A wrapped integer that represents the lower bound of the range | ||
* @param to A wrapped integer that represents the upper bound of the range | ||
* | ||
* @returns A thunk that returns a random integer in the chosen range | ||
*/ | ||
export function uniform( | ||
from: Wrapped<bigint>, | ||
to: Wrapped<bigint>, | ||
): Thunk<bigint> { | ||
return () => { | ||
const lower = unwrap(from); | ||
const upper = unwrap(to); | ||
|
||
const diff = upper - lower; | ||
|
||
if (diff > Number.MAX_SAFE_INTEGER) { | ||
throw new Error( | ||
`Range too large: ${lower}...${upper} (${diff} > ${Number.MAX_SAFE_INTEGER})`, | ||
) | ||
} | ||
|
||
return BigInt(Math.floor(Math.random() * Number(diff))) + lower; | ||
}; | ||
} |
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