Skip to content

Commit

Permalink
[d3-random] v1.1.0 (DefinitelyTyped#16467)
Browse files Browse the repository at this point in the history
* [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.
  • Loading branch information
tomwanzek authored and DABH committed May 15, 2017
1 parent 6c1df13 commit c7e6e41
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 49 deletions.
56 changes: 43 additions & 13 deletions types/d3-random/d3-random-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import * as d3Random from 'd3-random';

import * as seedrandom from 'seedrandom';

// ------------------------------------------------------------
// Preparatory Steps
Expand All @@ -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);
125 changes: 91 additions & 34 deletions types/d3-random/index.d.ts
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/tomwanzek>, Alex Ford <https://github.com/gustavderdrache>, Boris Yankov <https://github.com/borisyankov>
// 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;
4 changes: 2 additions & 2 deletions types/d3-random/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": false,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
Expand All @@ -19,4 +19,4 @@
"index.d.ts",
"d3-random-tests.ts"
]
}
}
6 changes: 6 additions & 0 deletions types/d3-random/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "dtslint/dt.json",
"rules": {
"max-line-length": [false, 140]
}
}

0 comments on commit c7e6e41

Please sign in to comment.