Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ml-random

Package Overview
Dependencies
Maintainers
5
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ml-random - npm Package Compare versions

Comparing version 0.3.0 to 0.4.0

5

History.md

@@ -0,1 +1,6 @@

<a name="0.4.0"></a>
# [0.4.0](https://github.com/mljs/random/compare/v0.3.0...v0.4.0) (2018-05-23)
<a name="0.3.0"></a>

@@ -2,0 +7,0 @@ # [0.3.0](https://github.com/mljs/random/compare/v0.2.0...v0.3.0) (2018-05-23)

@@ -26,5 +26,33 @@ // tslint:disable-next-line

}
/**
* Draw a random number from a uniform distribution on [0,1)
* @return The random number
*/
random() {
return this.randomGenerator();
}
/**
* Draw a random integer from a uniform distribution on [low, high). If only low is specified, the number is drawn on [0, low)
* @param low - The lower bound of the uniform distribution interval.
* @param high - The higher bound of the uniform distribution interval.
*/
randInt(low, high) {
if (high === undefined) {
high = low;
low = 0;
}
return low + Math.floor(this.randomGenerator() * (high - low));
}
/**
* Draw several random number from a uniform distribution on [0, 1)
* @param size - The number of number to draw
* @return - The list of drawn numbers.
*/
randomSample(size) {
const result = [];
for (let i = 0; i < size; i++) {
result.push(this.random());
}
return result;
}
}

@@ -20,3 +20,19 @@ import { IChoiceOptions } from './Options';

choice(values: number, options?: IChoiceOptions): number[];
/**
* Draw a random number from a uniform distribution on [0,1)
* @return The random number
*/
random(): number;
/**
* Draw a random integer from a uniform distribution on [low, high). If only low is specified, the number is drawn on [0, low)
* @param low - The lower bound of the uniform distribution interval.
* @param high - The higher bound of the uniform distribution interval.
*/
randInt(low: number, high?: number): number;
/**
* Draw several random number from a uniform distribution on [0, 1)
* @param size - The number of number to draw
* @return - The list of drawn numbers.
*/
randomSample(size: number): number[];
}

@@ -28,6 +28,34 @@ "use strict";

}
/**
* Draw a random number from a uniform distribution on [0,1)
* @return The random number
*/
random() {
return this.randomGenerator();
}
/**
* Draw a random integer from a uniform distribution on [low, high). If only low is specified, the number is drawn on [0, low)
* @param low - The lower bound of the uniform distribution interval.
* @param high - The higher bound of the uniform distribution interval.
*/
randInt(low, high) {
if (high === undefined) {
high = low;
low = 0;
}
return low + Math.floor(this.randomGenerator() * (high - low));
}
/**
* Draw several random number from a uniform distribution on [0, 1)
* @param size - The number of number to draw
* @return - The list of drawn numbers.
*/
randomSample(size) {
const result = [];
for (let i = 0; i < size; i++) {
result.push(this.random());
}
return result;
}
}
exports.default = Random;

2

package.json
{
"name": "ml-random",
"version": "0.3.0",
"version": "0.4.0",
"description": "choose randomly from a selection of elements",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

@@ -99,1 +99,51 @@ import * as XSAdd from 'ml-xsadd';

});
describe('random.randomSample', () => {
it('should generate an array of random numbers', () => {
const numbers = random.randomSample(10);
expect(numbers).toHaveLength(10);
numbers.forEach(num => expect(num).toBeLessThan(1));
numbers.forEach(num => expect(num).toBeGreaterThanOrEqual(0));
expect(numbers).toMatchSnapshot();
});
});
describe('random.randInt', () => {
it('should generate integers within an range with uniform distribution', () => {
const n = 50000;
const low = 4;
const high = 8;
const values = [];
for (let i = 0; i < n; i++) {
values.push(random.randInt(low, high));
}
const expFreq = 1 / (high - low);
expect(freq(values, 4)).toBeCloseTo(expFreq);
expect(freq(values, 5)).toBeCloseTo(expFreq);
expect(freq(values, 6)).toBeCloseTo(expFreq);
expect(freq(values, 7)).toBeCloseTo(expFreq);
expect(freq(values, 3)).toEqual(0);
expect(freq(values, 8)).toEqual(0);
});
it('if only low is specified, low is 0 and high is low', () => {
const rand1 = new Random(12);
const rand2 = new Random(12);
const n = 20;
const values1 = [];
const values2 = [];
for (let i = 0; i < n; i++) {
values1.push(rand1.randInt(2));
values2.push(rand2.randInt(0, 2));
}
expect(values1).toEqual(values2);
});
});
function freq(arr: number[], n: number) {
return (
arr.reduce((prev, current) => (current === n ? prev + 1 : prev), 0) /
arr.length
);
}

@@ -43,5 +43,35 @@ // tslint:disable-next-line

/**
* Draw a random number from a uniform distribution on [0,1)
* @return The random number
*/
public random(): number {
return this.randomGenerator();
}
/**
* Draw a random integer from a uniform distribution on [low, high). If only low is specified, the number is drawn on [0, low)
* @param low - The lower bound of the uniform distribution interval.
* @param high - The higher bound of the uniform distribution interval.
*/
public randInt(low: number, high?: number): number {
if (high === undefined) {
high = low;
low = 0;
}
return low + Math.floor(this.randomGenerator() * (high - low));
}
/**
* Draw several random number from a uniform distribution on [0, 1)
* @param size - The number of number to draw
* @return - The list of drawn numbers.
*/
public randomSample(size: number): number[] {
const result = [];
for (let i = 0; i < size; i++) {
result.push(this.random());
}
return result;
}
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc