New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

ml-random

Package Overview
Dependencies
Maintainers
4
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 1.0.1 to 2.0.0

lib-esm/Random.d.ts

5

lib-esm/choice.js

@@ -24,4 +24,5 @@ const PROB_TOLERANCE = 0.00000001;

}
if (Math.abs(1 - cumSum[cumSum.length - 1]) > PROB_TOLERANCE) {
throw new Error(`probabilities should sum to 1, but instead sums to ${cumSum[cumSum.length - 1]}`);
//@ts-expect-error we know length is at least 1
if (Math.abs(1 - cumSum.at(-1)) > PROB_TOLERANCE) {
throw new Error(`probabilities should sum to 1, but instead sums to ${cumSum.at(-1)}`);
}

@@ -28,0 +29,0 @@ }

38

lib-esm/index.d.ts

@@ -1,40 +0,4 @@

import { ChoiceOptions } from './Options';
import { RandomNumberGenerator } from './RandomNumberGenerator';
/**
* @classdesc Random class
*/
export default class Random {
private randomGenerator;
/**
* @param [seedOrRandom=Math.random] - Control the random number generator used by the Random class instance. Pass a random number generator function with a uniform distribution over the half-open interval [0, 1[. If seed will pass it to ml-xsadd to create a seeded random number generator. If undefined will use Math.random.
*/
constructor(seedOrRandom?: RandomNumberGenerator | number);
/**
* Returns an array of elements choosen from a list
* @param values - The values to choose from. If a number, the list will be a range of integer from 0 to that number.
* @param options - option object
* @return The choosen values
*/
choice<T>(values: T[], options?: ChoiceOptions): T[];
choice(values: number, options?: ChoiceOptions): 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[];
}
export * from './Random';
export * from './Options';
export * from './RandomNumberGenerator';
//# sourceMappingURL=index.d.ts.map

@@ -1,59 +0,4 @@

import XSAdd from 'ml-xsadd';
import choice from './choice';
/**
* @classdesc Random class
*/
export default class Random {
/**
* @param [seedOrRandom=Math.random] - Control the random number generator used by the Random class instance. Pass a random number generator function with a uniform distribution over the half-open interval [0, 1[. If seed will pass it to ml-xsadd to create a seeded random number generator. If undefined will use Math.random.
*/
constructor(seedOrRandom = Math.random) {
if (typeof seedOrRandom === 'number') {
const xsadd = new XSAdd(seedOrRandom);
this.randomGenerator = xsadd.random;
}
else {
this.randomGenerator = seedOrRandom;
}
}
choice(values, options) {
if (typeof values === 'number') {
return choice(values, options, this.randomGenerator);
}
return choice(values, options, this.randomGenerator);
}
/**
* 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;
}
}
export * from './Random';
export * from './Options';
export * from './RandomNumberGenerator';
//# sourceMappingURL=index.js.map

@@ -26,4 +26,5 @@ "use strict";

}
if (Math.abs(1 - cumSum[cumSum.length - 1]) > PROB_TOLERANCE) {
throw new Error(`probabilities should sum to 1, but instead sums to ${cumSum[cumSum.length - 1]}`);
//@ts-expect-error we know length is at least 1
if (Math.abs(1 - cumSum.at(-1)) > PROB_TOLERANCE) {
throw new Error(`probabilities should sum to 1, but instead sums to ${cumSum.at(-1)}`);
}

@@ -30,0 +31,0 @@ }

@@ -1,40 +0,4 @@

import { ChoiceOptions } from './Options';
import { RandomNumberGenerator } from './RandomNumberGenerator';
/**
* @classdesc Random class
*/
export default class Random {
private randomGenerator;
/**
* @param [seedOrRandom=Math.random] - Control the random number generator used by the Random class instance. Pass a random number generator function with a uniform distribution over the half-open interval [0, 1[. If seed will pass it to ml-xsadd to create a seeded random number generator. If undefined will use Math.random.
*/
constructor(seedOrRandom?: RandomNumberGenerator | number);
/**
* Returns an array of elements choosen from a list
* @param values - The values to choose from. If a number, the list will be a range of integer from 0 to that number.
* @param options - option object
* @return The choosen values
*/
choice<T>(values: T[], options?: ChoiceOptions): T[];
choice(values: number, options?: ChoiceOptions): 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[];
}
export * from './Random';
export * from './Options';
export * from './RandomNumberGenerator';
//# sourceMappingURL=index.d.ts.map

@@ -16,65 +16,6 @@ "use strict";

};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ml_xsadd_1 = __importDefault(require("ml-xsadd"));
const choice_1 = __importDefault(require("./choice"));
/**
* @classdesc Random class
*/
class Random {
/**
* @param [seedOrRandom=Math.random] - Control the random number generator used by the Random class instance. Pass a random number generator function with a uniform distribution over the half-open interval [0, 1[. If seed will pass it to ml-xsadd to create a seeded random number generator. If undefined will use Math.random.
*/
constructor(seedOrRandom = Math.random) {
if (typeof seedOrRandom === 'number') {
const xsadd = new ml_xsadd_1.default(seedOrRandom);
this.randomGenerator = xsadd.random;
}
else {
this.randomGenerator = seedOrRandom;
}
}
choice(values, options) {
if (typeof values === 'number') {
return (0, choice_1.default)(values, options, this.randomGenerator);
}
return (0, choice_1.default)(values, options, this.randomGenerator);
}
/**
* 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;
__exportStar(require("./Random"), exports);
__exportStar(require("./Options"), exports);
__exportStar(require("./RandomNumberGenerator"), exports);
//# sourceMappingURL=index.js.map
{
"name": "ml-random",
"version": "1.0.1",
"version": "2.0.0",
"description": "Choose randomly from a selection of elements",

@@ -22,3 +22,3 @@ "main": "lib/index.js",

"test": "npm run test-only && npm run eslint && npm run prettier && npm run check-types",
"test-only": "jest --coverage",
"test-only": "vitest run --coverage",
"tsc": "npm run clean && npm run tsc-cjs && npm run tsc-esm",

@@ -40,15 +40,15 @@ "tsc-cjs": "tsc --project tsconfig.cjs.json",

"dependencies": {
"ml-xsadd": "^2.0.0"
"ml-xsadd": "^3.0.1"
},
"devDependencies": {
"@babel/plugin-transform-modules-commonjs": "^7.21.5",
"@babel/preset-typescript": "^7.21.5",
"@types/jest": "^29.5.1",
"eslint": "^8.40.0",
"eslint-config-cheminfo-typescript": "^11.3.1",
"jest": "^29.5.0",
"prettier": "^2.8.8",
"rimraf": "^5.0.0",
"typescript": "^5.0.4"
"@babel/plugin-transform-modules-commonjs": "^7.25.7",
"@babel/preset-typescript": "^7.25.7",
"@vitest/coverage-v8": "^2.1.3",
"eslint": "^9.12.0",
"eslint-config-cheminfo-typescript": "^16.0.0",
"prettier": "^3.3.3",
"rimraf": "^6.0.1",
"typescript": "^5.6.3",
"vitest": "^2.1.3"
}
}
# ml-random
[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![build status][ci-image]][ci-url]
[![Test coverage][codecov-image]][codecov-url]

@@ -17,3 +17,3 @@ [![npm download][download-image]][download-url]

```js
const Random = require('ml-random').default;
const { Random } = require('ml-random');
```

@@ -29,4 +29,4 @@

[npm-url]: https://www.npmjs.com/package/ml-random
[travis-image]: https://img.shields.io/travis/mljs/random/master.svg?style=flat-square
[travis-url]: https://travis-ci.org/mljs/random
[ci-image]: https://github.com/mljs/random/workflows/Node.js%20CI/badge.svg?branch=main
[ci-url]: https://github.com/mljs/random/actions?query=workflow%3A%22Node.js+CI%22
[codecov-image]: https://img.shields.io/codecov/c/github/mljs/random.svg?style=flat-square

@@ -33,0 +33,0 @@ [codecov-url]: https://codecov.io/gh/mljs/random

@@ -48,7 +48,6 @@ import { ChoiceOptions } from './Options';

if (Math.abs(1 - cumSum[cumSum.length - 1]) > PROB_TOLERANCE) {
//@ts-expect-error we know length is at least 1
if (Math.abs(1 - cumSum.at(-1)) > PROB_TOLERANCE) {
throw new Error(
`probabilities should sum to 1, but instead sums to ${
cumSum[cumSum.length - 1]
}`,
`probabilities should sum to 1, but instead sums to ${cumSum.at(-1)}`,
);

@@ -55,0 +54,0 @@ }

@@ -1,79 +0,3 @@

import XSAdd from 'ml-xsadd';
import { ChoiceOptions } from './Options';
import { RandomNumberGenerator } from './RandomNumberGenerator';
import choice from './choice';
/**
* @classdesc Random class
*/
export default class Random {
private randomGenerator: RandomNumberGenerator;
/**
* @param [seedOrRandom=Math.random] - Control the random number generator used by the Random class instance. Pass a random number generator function with a uniform distribution over the half-open interval [0, 1[. If seed will pass it to ml-xsadd to create a seeded random number generator. If undefined will use Math.random.
*/
constructor(seedOrRandom: RandomNumberGenerator | number = Math.random) {
if (typeof seedOrRandom === 'number') {
const xsadd = new XSAdd(seedOrRandom);
this.randomGenerator = xsadd.random;
} else {
this.randomGenerator = seedOrRandom;
}
}
/**
* Returns an array of elements choosen from a list
* @param values - The values to choose from. If a number, the list will be a range of integer from 0 to that number.
* @param options - option object
* @return The choosen values
*/
public choice<T>(values: T[], options?: ChoiceOptions): T[];
public choice(values: number, options?: ChoiceOptions): number[];
public choice<T>(
values: T[] | number,
options?: ChoiceOptions,
): Array<T | number> {
if (typeof values === 'number') {
return choice(values, options, this.randomGenerator);
}
return choice(values, options, this.randomGenerator);
}
/**
* 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: number[] = [];
for (let i = 0; i < size; i++) {
result.push(this.random());
}
return result;
}
}
export * from './Random';
export * from './Options';
export * from './RandomNumberGenerator';

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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