From c7e6e41cd497a3015f08ab626aadec589fe2911e Mon Sep 17 00:00:00 2001 From: Tom Wanzek Date: Fri, 12 May 2017 10:13:33 -0400 Subject: [PATCH] [d3-random] v1.1.0 (#16467) * [Feature] Update d3-random to minor version 1.1 adding configurable source of pseudo-randomnumbers. * [Chore] Enable `strictNullChecks` * [Chore] Add tslint config. * [Chore] Bumped version. --- types/d3-random/d3-random-tests.ts | 56 ++++++++++--- types/d3-random/index.d.ts | 125 +++++++++++++++++++++-------- types/d3-random/tsconfig.json | 4 +- types/d3-random/tslint.json | 6 ++ 4 files changed, 142 insertions(+), 49 deletions(-) create mode 100644 types/d3-random/tslint.json diff --git a/types/d3-random/d3-random-tests.ts b/types/d3-random/d3-random-tests.ts index 8cd72d9f0e875ef..7385d3e984a4b2e 100644 --- a/types/d3-random/d3-random-tests.ts +++ b/types/d3-random/d3-random-tests.ts @@ -7,7 +7,7 @@ */ import * as d3Random from 'd3-random'; - +import * as seedrandom from 'seedrandom'; // ------------------------------------------------------------ // Preparatory Steps @@ -19,40 +19,70 @@ let randomNumberGenerator: () => number; // randomUniform // ------------------------------------------------------------ -randomNumberGenerator = d3Random.randomUniform(); -randomNumberGenerator = d3Random.randomUniform(0.2); -randomNumberGenerator = d3Random.randomUniform(0.2, 5); +let prngUniform: d3Random.RandomUniform; + +prngUniform = d3Random.randomUniform; +prngUniform = d3Random.randomUniform.source(seedrandom("Schroedinger's flea.")); + +randomNumberGenerator = prngUniform(); +randomNumberGenerator = prngUniform(0.2); +randomNumberGenerator = prngUniform(0.2, 5); // ------------------------------------------------------------ // randomNormal // ------------------------------------------------------------ -randomNumberGenerator = d3Random.randomNormal(); -randomNumberGenerator = d3Random.randomNormal(3); -randomNumberGenerator = d3Random.randomNormal(3, 4); +let prngNormal: d3Random.RandomNormal; + +prngNormal = d3Random.randomNormal; +prngNormal = d3Random.randomNormal.source(seedrandom("Schroedinger's flea.")); + +randomNumberGenerator = prngNormal(); +randomNumberGenerator = prngNormal(3); +randomNumberGenerator = prngNormal(3, 4); // ------------------------------------------------------------ // randomLogNormal // ------------------------------------------------------------ -randomNumberGenerator = d3Random.randomLogNormal(); -randomNumberGenerator = d3Random.randomLogNormal(3); -randomNumberGenerator = d3Random.randomLogNormal(3, 4); +let prngLogNormal: d3Random.RandomLogNormal; + +prngLogNormal = d3Random.randomLogNormal; +prngLogNormal = d3Random.randomLogNormal.source(seedrandom("Schroedinger's flea.")); + +randomNumberGenerator = prngLogNormal(); +randomNumberGenerator = prngLogNormal(3); +randomNumberGenerator = prngLogNormal(3, 4); // ------------------------------------------------------------ // randomBates // ------------------------------------------------------------ -randomNumberGenerator = d3Random.randomBates(3); +let prngBates: d3Random.RandomBates; + +prngBates = d3Random.randomBates; +prngBates = d3Random.randomBates.source(seedrandom("Schroedinger's flea.")); + +randomNumberGenerator = prngBates(3); // ------------------------------------------------------------ // randomIrwinHall // ------------------------------------------------------------ -randomNumberGenerator = d3Random.randomIrwinHall(3); +let prngIrwinHall: d3Random.RandomIrwinHall; + +prngIrwinHall = d3Random.randomIrwinHall; +prngIrwinHall = d3Random.randomIrwinHall.source(seedrandom("Schroedinger's flea.")); + +randomNumberGenerator = prngIrwinHall(3); // ------------------------------------------------------------ // randomExponential // ------------------------------------------------------------ -randomNumberGenerator = d3Random.randomExponential(1 / 40); +let prngExponential: d3Random.RandomExponential; + +prngExponential = d3Random.randomExponential; +prngExponential = d3Random.randomExponential.source(seedrandom("Schroedinger's flea.")); + +randomNumberGenerator = prngExponential(1 / 40); diff --git a/types/d3-random/index.d.ts b/types/d3-random/index.d.ts index 48c9b245edb5b72..41b60342151b1a5 100644 --- a/types/d3-random/index.d.ts +++ b/types/d3-random/index.d.ts @@ -1,55 +1,112 @@ -// Type definitions for D3JS d3-random module v1.0.1 +// Type definitions for D3JS d3-random module 1.1 // Project: https://github.com/d3/d3-random/ // Definitions by: Tom Wanzek , Alex Ford , Boris Yankov // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// Last module patch version validated against: 1.1.0 + +export interface RandomNumberGenerationSource { + /** + * Returns the same type of function for generating random numbers but where the given random number + * generator source is used as the source of randomness instead of Math.random. + * This is useful when a seeded random number generator is preferable to Math.random. + * + * @param source Source (pseudo-)random number generator implementing the Math.random interface. + * The given random number generator must implement the same interface as Math.random and + * only return values in the range [0, 1). + */ + source(source: () => number): this; +} + /** - * Returns a function for generating random numbers with a uniform distribution). - * The minimum allowed value of a returned number is min, and the maximum is max. - * If min is not specified, it defaults to 0; if max is not specified, it defaults to 1. - * - * @param min The minimum allowed value of a returned number, defaults to 0. - * @param max The maximum allowed value of a returned number, defaults to 1. + * A configurable random number generator for the uniform distribution. */ -export function randomUniform(min?: number, max?: number): () => number; +export interface RandomUniform extends RandomNumberGenerationSource { + /** + * Returns a function for generating random numbers with a uniform distribution). + * The minimum allowed value of a returned number is min, and the maximum is max. + * If min is not specified, it defaults to 0; if max is not specified, it defaults to 1. + * + * @param min The minimum allowed value of a returned number, defaults to 0. + * @param max The maximum allowed value of a returned number, defaults to 1. + */ + (min?: number, max?: number): () => number; +} + +export const randomUniform: RandomUniform; /** - * Returns a function for generating random numbers with a normal (Gaussian) distribution. - * The expected value of the generated numbers is mu, with the given standard deviation sigma. - * If mu is not specified, it defaults to 0; if sigma is not specified, it defaults to 1. - * - * @param mu Expected value, defaults to 0. - * @param sigma Standard deviation, defaults to 1. + * A configurable random number generator for the normal (Gaussian) distribution. */ -export function randomNormal(mu?: number, sigma?: number): () => number; +export interface RandomNormal extends RandomNumberGenerationSource { + /** + * Returns a function for generating random numbers with a normal (Gaussian) distribution. + * The expected value of the generated numbers is mu, with the given standard deviation sigma. + * If mu is not specified, it defaults to 0; if sigma is not specified, it defaults to 1. + * + * @param mu Expected value, defaults to 0. + * @param sigma Standard deviation, defaults to 1. + */ + (mu?: number, sigma?: number): () => number; +} + +export const randomNormal: RandomNormal; /** - * Returns a function for generating random numbers with a log-normal distribution. The expected value of the random variable’s natural logrithm is mu, - * with the given standard deviation sigma. If mu is not specified, it defaults to 0; if sigma is not specified, it defaults to 1. - * - * @param mu Expected value, defaults to 0. - * @param sigma Standard deviation, defaults to 1. + * A configurable random number generator for the log-normal distribution. */ -export function randomLogNormal(mu?: number, sigma?: number): () => number; +export interface RandomLogNormal extends RandomNumberGenerationSource { + /** + * Returns a function for generating random numbers with a log-normal distribution. The expected value of the random variable’s natural logrithm is mu, + * with the given standard deviation sigma. If mu is not specified, it defaults to 0; if sigma is not specified, it defaults to 1. + * + * @param mu Expected value, defaults to 0. + * @param sigma Standard deviation, defaults to 1. + */ + (mu?: number, sigma?: number): () => number; +} + +export const randomLogNormal: RandomLogNormal; /** - * Returns a function for generating random numbers with a Bates distribution with n independent variables. - * - * @param n Number of independent random variables to use. + * A configurable random number generator for the Bates distribution. */ -export function randomBates(n: number): () => number; +export interface RandomBates extends RandomNumberGenerationSource { + /** + * Returns a function for generating random numbers with a Bates distribution with n independent variables. + * + * @param n Number of independent random variables to use. + */ + (n: number): () => number; +} + +export const randomBates: RandomBates; /** - * Returns a function for generating random numbers with an Irwin–Hall distribution with n independent variables. - * - * @param n Number of independent random variables to use. + * A configurable random number generator for the Irwin–Hall distribution. */ -export function randomIrwinHall(n: number): () => number; +export interface RandomIrwinHall extends RandomNumberGenerationSource { + /** + * Returns a function for generating random numbers with an Irwin–Hall distribution with n independent variables. + * + * @param n Number of independent random variables to use. + */ + (n: number): () => number; +} + +export const randomIrwinHall: RandomIrwinHall; /** - * Returns a function for generating random numbers with an exponential distribution with the rate lambda; - * equivalent to time between events in a Poisson process with a mean of 1 / lambda. - * - * @param lambda Expected time between events. + * A configurable random number generator for the exponential distribution. */ -export function randomExponential(lambda: number): () => number; +export interface RandomExponential extends RandomNumberGenerationSource { + /** + * Returns a function for generating random numbers with an exponential distribution with the rate lambda; + * equivalent to time between events in a Poisson process with a mean of 1 / lambda. + * + * @param lambda Expected time between events. + */ + (lambda: number): () => number; +} + +export const randomExponential: RandomExponential; diff --git a/types/d3-random/tsconfig.json b/types/d3-random/tsconfig.json index 20a20f04f16bce3..f9544a05277db4e 100644 --- a/types/d3-random/tsconfig.json +++ b/types/d3-random/tsconfig.json @@ -6,7 +6,7 @@ ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "baseUrl": "../", "typeRoots": [ "../" @@ -19,4 +19,4 @@ "index.d.ts", "d3-random-tests.ts" ] -} \ No newline at end of file +} diff --git a/types/d3-random/tslint.json b/types/d3-random/tslint.json new file mode 100644 index 000000000000000..4ae99abce40a0bd --- /dev/null +++ b/types/d3-random/tslint.json @@ -0,0 +1,6 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + "max-line-length": [false, 140] + } +}