js-randomness-predictor
v4.1.0
Published
Predict Math.random output in Node, Deno, Bun, Chrome, Firefox, and Safari
Maintainers
Readme
Quick Start
Always use the predictor that matches the environment where the original sequence was generated.
import JSRandomnessPredictor from "js-randomness-predictor";
const predictor = JSRandomnessPredictor.node();
const nextPrediction = await predictor.predictNext();
console.log(nextPrediction);Installation
Node
npm i js-randomness-predictor
yarn add js-randomness-predictor
pnpm add js-randomness-predictorBun
bun add js-randomness-predictorDeno
deno add npm:js-randomness-predictorUsage
ESM
import JSRandomnessPredictor from "js-randomness-predictor";CJS
const JSRandomnessPredictor = require("js-randomness-predictor");Deno
import JSRandomnessPredictor from "npm:js-randomness-predictor";Browser
Browser usage requires additional setup: browser usage guide.
Predictors
| Runtime | Auto Generate Sequence | Sequence Length | Known Issues | | ------------------- | ---------------------- | :-------------: | :-----------------------------------------------------------------------------------------------------------: | | Node | ✅ | 5 | Link | | Bun | ✅ Native Bun only | 6 | Link | | Deno | ✅ Native Deno only | 4 | - | | Chrome | ❌ | 4 | Link | | Firefox | ❌ | 4 | Link | | Safari | ❌ | 6 | Link |
Node
Generate a sequence automatically:
const predictor = JSRandomnessPredictor.node();
const prediction = await predictor.predictNext();
const isAccurate = prediction === Math.random();Provide your own sequence:
const sequence = Array.from({ length: 5 }, Math.random);
const predictor = JSRandomnessPredictor.node(sequence);
const prediction = await predictor.predictNext();
const isAccurate = prediction === Math.random();Bun
- If running natively in Bun, a sequence can be generated automatically.
- If using the Bun predictor outside of Bun, you must provide a sequence that was originally generated in Bun.
const predictor = JSRandomnessPredictor.bun();
const prediction = await predictor.predictNext();
const isAccurate = prediction === Math.random();Provide your own sequence:
const sequence = [Math.random(), Math.random(), Math.random(), Math.random(), Math.random(), Math.random()];
const predictor = JSRandomnessPredictor.bun(sequence);
const prediction = await predictor.predictNext();
const isAccurate = prediction === Math.random();Deno
- If running natively in Deno, a sequence can be generated automatically.
- If using the Deno predictor outside of Deno, you must provide a sequence that was originally generated in Deno.
const predictor = JSRandomnessPredictor.deno();
const prediction = await predictor.predictNext();
const isAccurate = prediction === Math.random();Provide your own sequence:
const sequence = Array.from({ length: 4 }, Math.random);
const predictor = JSRandomnessPredictor.deno(sequence);
const prediction = await predictor.predictNext();
const isAccurate = prediction === Math.random();Chrome
const predictor = JSRandomnessPredictor.chrome(sequence);
const prediction = await predictor.predictNext();Firefox
const predictor = JSRandomnessPredictor.firefox(sequence);
const prediction = await predictor.predictNext();Safari
const predictor = JSRandomnessPredictor.safari(sequence);
const prediction = await predictor.predictNext();Command Line Interface
Installation
Global
# Install globally (system wide)
npm i -g js-randomness-predictor
# Use from anywhere
js-randomness-predictor [options]Local
# Add as a local project dependency
npm i js-randomness-predictor
# Manually 'path' to CLI
node_modules/.bin/js-randomness-predictor [options]Basic Usage
# Options
js-randomness-predictor
-e <environment>
[-s <sequence...>]
[-p <num_predictions>]
[-x <export_path>]
[-f <force_export>]
# Help
js-randomness-predictor --help
# Version
js-randomness-predictor --versionRuntime Selection
- By default, the CLI executes in Node.js.
- Set environment variable
JSRP_RUNTIMEto run the CLI in Bun or Deno.
Bun
JSRP_RUNTIME=bun js-randomness-predictor -e bun
JSRP_RUNTIME=bun js-randomness-predictor -e deno --sequence ...Deno
JSRP_RUNTIME=deno js-randomness-predictor -e deno
JSRP_RUNTIME=deno js-randomness-predictor -e bun --sequence ...Windows
cmd:
set JSRP_RUNTIME=bun && js-randomness-predictor [...]PowerShell:
$env:JSRP_RUNTIME = "bun" ; js-randomness-predictor [...]Exporting Results
Use --export (or -x) to write results to a JSON file.
- Path is relative to the current working directory.
- File must have a
.jsonextension. - Existing files are not overwritten unless
--forceis used. - Missing directories are not created unless
--forceis used.
Examples
Node
js-randomness-predictor -e node
js-randomness-predictor -e node -s 1 2 3 4 5
js-randomness-predictor -e node -s 1 2 3 4 5 -p 15Chrome
js-randomness-predictor -e chrome -s 1 2 3 4
js-randomness-predictor -e chrome -s 1 2 3 4 -p 5Firefox
js-randomness-predictor -e firefox -s 1 2 3 4Safari
js-randomness-predictor -e safari -s 1 2 3 4 5 6Bun
js-randomness-predictor -e bun -s 1 2 3 4 5 6Deno
js-randomness-predictor -e deno -s 1 2 3 4