-
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transducers-binary): add randomBits(), update readme
- Loading branch information
1 parent
97f6680
commit 36ca046
Showing
4 changed files
with
83 additions
and
45 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
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,17 @@ | ||
import { IRandom, SYSTEM } from "@thi.ng/random"; | ||
import { repeatedly } from "@thi.ng/transducers"; | ||
|
||
/** | ||
* Returns an iterator of random bits, with 1's occurring w/ given | ||
* probability `prob` (in the [0,1] interval). If `num` is given, only | ||
* that many bits will be produced. | ||
* | ||
* By default, uses system PRNG, but a custom `IRandom` impl can be | ||
* provided via `rnd` arg. | ||
* | ||
* @param prob | ||
* @param num | ||
* @param rnd | ||
*/ | ||
export const randomBits = (prob: number, num?: number, rnd: IRandom = SYSTEM) => | ||
repeatedly(() => (rnd.float() < prob ? 1 : 0), num); |