Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring random-js back up to speed with newer webpack and node #65

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "npm" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "daily"
open-pull-requests-limit: 0
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ node_modules
.DS_Store
coverage
.rpt2_cache
dist
dist

yarn.lock
package-lock.json
bun.lockb
4 changes: 4 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"esversion": 6,
"node": true
}
28 changes: 28 additions & 0 deletions .markdownlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# MD013/line-length - Line length
MD013:
# Number of characters
line_length: 450
# Number of characters for headings
heading_line_length: 450
# Number of characters for code blocks
code_block_line_length: 450
# Include code blocks
code_blocks: true
# Include tables
tables: true
# Include headings
headings: true
# Include headings
headers: true
# Strict length checking
strict: false
# Stern length checking
stern: false

# MD037/no-space-in-emphasis - Spaces inside emphasis markers
MD037: false

# MD046/code-block-style - Code block style
MD046:
# Block style
style: "consistent"
34 changes: 18 additions & 16 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
# 2.1.0 (2019-05-30)
# Changelog

## 2.1.0 (2019-05-30)

- Changed `dist/` outputs:
- `random-js.js` => `random-js.umd.js`
- `random-js.mjs` => `random-js.esm.js`
- `random-js.min.js` => `random-js.umd.min.js`

# 2.0.0 (2019-04-01)
## 2.0.0 (2019-04-01)

- Release 2.0.0 (no code changes from 2.0.0-rc5)

# 2.0.0-rc5 (2019-03-15)
## 2.0.0-rc5 (2019-03-15)

- Add sourcemaps

# 2.0.0-rc4 (2019-03-15)
## 2.0.0-rc4 (2019-03-15)

- Improve minified bundle size
- Add `"sideEffects": false` to `package.json` as a hint to Webpack

# 2.0.0-rc3 (2019-03-10)
## 2.0.0-rc3 (2019-03-10)

- Provide a binding for `try`-`catch` to help parsing issues [#30](https://github.com/ckknight/random-js/issues/30)

# 2.0.0-rc2 (2019-02-20)
## 2.0.0-rc2 (2019-02-20)

- Added `nodeCrypto` engine
- Exposed `createEntropy` function, which can be used when seeding a Mersenne Twister
- Changed `browserCrypto` to no longer be `null` if outside of a browser context, instead `throw`ing on first use.

# 2.0.0-rc1 (2019-02-17)
## 2.0.0-rc1 (2019-02-17)

- Converted to TypeScript, with type definitions available.
- Using ESNext-style modules (though still available as UMD)
Expand All @@ -37,25 +39,25 @@
- Using rollup to package the build
- Changed .travis.yml node versions

# 1.0.8 (2015-10-13)
## 1.0.8 (2015-10-13)

- add `getUseCount()` to the `mt19937` engine, which returns how many requests or discards have occurred.

# 1.0.7 (2015-10-13)
## 1.0.7 (2015-10-13)

- the current time is now used in `Random.generateEntropyArray`

# 1.0.6 (2015-09-28)
## 1.0.6 (2015-09-28)

- removed the optionalDependencies in package.json "benchmark" and "microtime".

# 1.0.5 (2015-03-03)
## 1.0.5 (2015-03-03)

- `sample()` results are no longer biased toward the final element.
- `discard(count)` on the `mt19937` engine properly works when count is less than 625
- Clarify that the built-in engines return signed 32-bit integers (not unsigned).

# 1.0.4 (2014-05-11)
## 1.0.4 (2014-05-11)

- `string('')` now throws an error
- `hex(upper)` now returns a cached function, for efficiency.
Expand All @@ -64,20 +66,20 @@
- add benchmark suite, covers engines, integer, bool, and real.
- add `date(start, end)`, which returns a Date within a uniform distribution.

# 1.0.3 (2014-04-23)
## 1.0.3 (2014-04-23)

- `Random` no longer needs to be called with new, allowing `var r = require('random-js')()`
- Add `begin` and `end` arguments to `pick` and `picker`.

# 1.0.2 (2014-03-20)
## 1.0.2 (2014-03-20)

- Even if `shuffle` receives an empty array, it should return that same array.

# 1.0.1 (2014-03-20)
## 1.0.1 (2014-03-20)

- Fix `sample` and `shuffle` when they are passed an empty array.
- Include `-c` (compress) option when uglifying.

# 1.0.0 (2014-02-16)
## 1.0.0 (2014-02-16)

Initial release
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) Cameron Knight (ckknight@gmail.com)
Copyright (c) Summer Knight (ckknight@gmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
# Random.js

[![Build Status](https://travis-ci.org/ckknight/random-js.svg?branch=master)](https://travis-ci.org/ckknight/random-js)
[![CDNJS](https://img.shields.io/cdnjs/v/random-js.svg)](https://cdnjs.com/libraries/random-js)

This is designed to be a mathematically correct random number generator library for JavaScript.
You can use it several times to get familiar with the parameters.

Inspiration was primarily taken from C++11's `<random>`.

## Upgrading from 1.0

Upgrading from 1.0 to 2.0 is a major, breaking change. For the most part, the way exports are defined is different. Instead of everything being available as static properties on a class-like function, random-js 2.0 exports each binding in accordance with current ECMAScript standards.

## Why is this needed?

Despite `Math.random()` being capable of producing numbers within [0, 1), there are a few downsides to doing so:
Despite `Math.random()` being capable of producing numbers within [0, 1], there are a few downsides to doing so:

- It is inconsistent between engines as to how many bits of randomness:
- Internet Explorer: 53 bits
Expand All @@ -36,6 +39,7 @@ One is also free to implement their own engine as long as it returns 32-bit inte

Some common, biased, _incorrect_ tool for generating random integers is as follows:

```js
// DO NOT USE, BIASED LOGIC
function randomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
Expand All @@ -45,6 +49,7 @@ Some common, biased, _incorrect_ tool for generating random integers is as follo
var i = (Math.random() * 32768) >>> 0;
return (i % (min - max)) + min;
}
```

The problem with both of these approaches is that the distribution of integers that it returns is not uniform. That is, it might be more biased to return `0` rather than `1`, making it inherently broken.

Expand Down Expand Up @@ -82,7 +87,7 @@ Any object that fulfills that interface is an `Engine`.
- `mt.discard(count)`: Discard `count` random values. More efficient than running `mt.next()` repeatedly.
- `mt.getUseCount()`: Return the number of times the engine has been used plus the number of discarded values.

One can seed a Mersenne Twister with the same value (`MersenneTwister19937.seed(value)`) or values (`MersenneTwister19937.seedWithArray(array)`) and discard the number of uses (`mt.getUseCount()`) to achieve the exact same state.
One can seed a Mersenne Twister with the same value (`MersenneTwister19937.seed(value)`) or values (`MersenneTwister19937.seedWithArray(array)`) and discard the number of uses (`mt.discard(count)`) to achieve the exact same state.

If you wish to know the initial seed of `MersenneTwister19937.autoSeed()`, it is recommended to use the `createEntropy()` function to create the seed manually (this is what `autoSeed` does under-the-hood).

Expand Down
9 changes: 4 additions & 5 deletions benchmark/engines.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/*jshint node:true*/
"use strict";

var Benchmark = require("benchmark");
var { nativeMath, browserCrypto, MersenneTwister19937 } = require("..");
let Benchmark = require("benchmark");
let { nativeMath, browserCrypto, MersenneTwister19937 } = require("..");

var suite = new Benchmark.Suite();
let suite = new Benchmark.Suite();

var mt19937 = MersenneTwister19937.autoSeed();
let mt19937 = MersenneTwister19937.autoSeed();

suite
.add("engines.nativeMath", () => nativeMath.next())
Expand Down
20 changes: 10 additions & 10 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
preset: 'ts-jest',
testEnvironment: 'node',
clearMocks: true,
resetMocks: true,
restoreMocks: true,
globals: {
"ts-jest": {
tsConfig: {
target: "es3",
module: "commonjs",
rootDir: "./src"
}
}
transform: {
'^.+\\.ts?$': ['ts-jest', {
tsconfig: {
target: 'es5',
module: 'commonjs',
rootDir: './src',
},
},]
}
};
27 changes: 13 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "A mathematically correct random number generator library for JavaScript.",
"version": "2.1.0",
"author": {
"name": "Cameron Kenneth Knight",
"name": "Summer Knight",
"email": "ckknight@gmail.com"
},
"keywords": [
Expand All @@ -22,27 +22,26 @@
],
"readmeFilename": "README.md",
"devDependencies": {
"@types/jest": "^24.0.5",
"@types/node": "^11.9.4",
"@types/jest": "^29.5.5",
"@types/node": "20.6.2",
"benchmark": "^2.1.4",
"jest": "^24.1.0",
"prettier": "^1.16.4",
"rimraf": "^2.6.3",
"rollup": "^1.2.1",
"rollup-plugin-typescript": "^1.0.0",
"rollup-plugin-typescript2": "^0.19.2",
"terser": "^3.16.1",
"ts-jest": "^23.10.5",
"tslint": "^5.12.1",
"jest": "^29.7.0",
"prettier": "^3.0.3",
"rimraf": "^5.0.1",
"rollup": "^3.29.2",
"@rollup/plugin-typescript": "^11.1.3",
"rollup-plugin-typescript2": "^0.35.0",
"terser": "^5.19.4",
"ts-jest": "^29.1.1",
"tslint-config-prettier": "^1.18.0",
"typescript": "^3.3.3"
"typescript": "^5.2.2"
},
"scripts": {
"clean": "rimraf dist",
"build:umd": "rollup -c rollup.config.es3.js --format umd -o dist/random-js.umd.js --name Random -m",
"build:esm": "rollup -c rollup.config.js --format esm --sourcemap true -o dist/random-js.esm.js",
"prebuild": "yarn clean",
"build": "yarn build:umd && yarn build:esm",
"build": "yarn build:umd --bundleConfigAsCjs && yarn build:esm --bundleConfigAsCjs",
"minify": "terser --source-map content=dist/random-js.umd.js.map --compress --mangle --output dist/random-js.umd.min.js dist/random-js.umd.js",
"postbuild": "yarn minify",
"benchmark": "for k in benchmark/*.js; do node $k; done",
Expand Down
10 changes: 10 additions & 0 deletions renovate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"lockFileMaintenance": {
"enabled": true,
},
"automerge": true,
"extends": [
"config:recommended"
]
}
2 changes: 1 addition & 1 deletion rollup.config.es3.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default {
typescript({
tsconfigOverride: {
compilerOptions: {
target: "es3",
target: "es5",
declaration: false,
declarationMap: false
},
Expand Down
38 changes: 37 additions & 1 deletion src/Random.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { int32 } from "./distribution/int32";
import { int53 } from "./distribution/int53";
import { int53Full } from "./distribution/int53Full";
import { integer } from "./distribution/integer";
import { max } from "./distribution/max";
import { min } from "./distribution/min";
import { pick } from "./distribution/pick";
import { real } from "./distribution/real";
import { realZeroToOneExclusive } from "./distribution/realZeroToOneExclusive";
Expand Down Expand Up @@ -34,6 +36,8 @@ jest.mock("./distribution/uuid4");
jest.mock("./distribution/string");
jest.mock("./distribution/hex");
jest.mock("./distribution/date");
jest.mock("./distribution/max");
jest.mock("./distribution/min");
jest.mock("./distribution/int32");
jest.mock("./distribution/int53");
jest.mock("./distribution/int53Full");
Expand Down Expand Up @@ -295,6 +299,38 @@ describe("Random", () => {
});
});

describe("max", () => {
it("calls max distribution", () => {
const minimum = 1234;
const maximum = 2345;
const dummy = 1337;
const spy = jest.fn().mockReturnValue(dummy);
(max as jest.Mock).mockReturnValue(spy);

const actual = random.max(minimum, maximum);

expect(max).toHaveBeenCalledWith(minimum, maximum);
expect(spy).toHaveBeenCalledWith(engine);
expect(actual).toBe(dummy);
});
});

describe("min", () => {
it("calls min distribution", () => {
const minimum = 1234;
const maximum = 2345;
const dummy = 1337;
const spy = jest.fn().mockReturnValue(dummy);
(min as jest.Mock).mockReturnValue(spy);

const actual = random.min(minimum, maximum);

expect(min).toHaveBeenCalledWith(minimum, maximum);
expect(spy).toHaveBeenCalledWith(engine);
expect(actual).toBe(dummy);
});
});

const SIMPLE_METHODS = {
int32,
int53,
Expand All @@ -305,7 +341,7 @@ describe("Random", () => {
uint53,
uint53Full
};
(Object.keys(SIMPLE_METHODS) as Array<keyof typeof SIMPLE_METHODS>).forEach(
(Object.keys(SIMPLE_METHODS) as (keyof typeof SIMPLE_METHODS)[]).forEach(
methodName => {
const distribution: Distribution = SIMPLE_METHODS[methodName];
describe(methodName, () => {
Expand Down
Loading