-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { RandomGenerator } from '@grandom/core' | ||
|
||
export default class RandomWeighted extends RandomGenerator { | ||
/** | ||
* Returns a value according the weights. | ||
* | ||
* @param weightsWithValues An array of weight-value pairs. | ||
*/ | ||
weighted <T extends Array<[number, any]>> (weightsWithValues: T): T[number][1] | ||
|
||
/** | ||
* Returns a value according the weights. | ||
* | ||
* @param values An array of values. | ||
* @param weights An array of weights. | ||
*/ | ||
weighted <T> (values: T[], weights: number[]): T | ||
|
||
weighted (arg1: any, arg2?: any): any { | ||
if (typeof arg1 !== 'undefined') { | ||
const weights: number[] = [] | ||
const elements: any[] = [] | ||
|
||
// process weightsWithValues ----------------------------------------------------------------- | ||
if (typeof arg2 === 'undefined') { | ||
if (!Array.isArray(arg1)) { | ||
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions | ||
throw new Error(`weightsWithValues must be an array, got: ${arg1} (typeof === '${typeof arg1}').`) | ||
} | ||
|
||
if (arg1.length < 1) { | ||
return | ||
} | ||
|
||
for (const weightWithValue of arg1) { | ||
const weight = weightWithValue[0] | ||
|
||
if (!(1 in weightWithValue)) { | ||
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions | ||
throw new Error(`value doesn't exist in ${weightWithValue}`) | ||
} | ||
|
||
const value = weightWithValue[1] | ||
|
||
weights.push(weight) | ||
elements.push(value) | ||
} | ||
|
||
return this._engine.nextWeighted(elements, weights) | ||
|
||
// process values, and weights --------------------------------------------------------------- | ||
} else { | ||
if (arg1.length < 1) { | ||
return | ||
} | ||
|
||
for (let i = 0; i < arg1.length; i++) { | ||
const value = arg1[i] | ||
const weight = arg2[i] | ||
|
||
weights.push(weight) | ||
elements.push(value) | ||
} | ||
|
||
return this._engine.nextWeighted(elements, weights) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import BasicEngine from '@grandom/engines/basic' | ||
import RandomWeighted from './RandomWeighted' | ||
|
||
const random = new RandomWeighted(new BasicEngine()) | ||
const weighted = random.weighted.bind(random) | ||
|
||
export default weighted |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { BasicEngine } from '@grandom/engines' | ||
import RandomWeighted from '../../src/RandomWeighted' | ||
|
||
const random = new RandomWeighted(new BasicEngine()) | ||
|
||
describe('RandomWeighted', () => { | ||
describe('.weighted()', () => { | ||
describe('weight-value pair array', () => { | ||
test('empty weight-value array', () => { | ||
expect(random.weighted([])).toBeUndefined() | ||
}) | ||
|
||
test('weight-value array of length === 1', () => { | ||
for (let i = 0; i < 10_000; i++) { | ||
expect(random.weighted([[1, 'a']])).toBe('a') | ||
} | ||
}) | ||
|
||
test('weight-value array of length === 2', () => { | ||
for (let i = 0; i < 10_000; i++) { | ||
expect(random.weighted([ | ||
[1, 'a'], | ||
[2, 3] | ||
])).toBeOneOf(['a', 3]) | ||
} | ||
}) | ||
|
||
test('weight-value array of length === 3', () => { | ||
for (let i = 0; i < 10_000; i++) { | ||
expect(random.weighted([ | ||
[1, 'a'], | ||
[2, 3], | ||
[3, false] | ||
])).toBeOneOf(['a', 3, false]) | ||
} | ||
}) | ||
}) | ||
|
||
describe('values array, weights array', () => { | ||
test('empty values, empty weights', () => { | ||
expect(random.weighted([], [])).toBeUndefined() | ||
}) | ||
|
||
test('1 pair of value-weight', () => { | ||
for (let i = 0; i < 10_000; i++) { | ||
expect(random.weighted(['a'], [1])).toBe('a') | ||
} | ||
}) | ||
|
||
test('2 pairs of value-weight', () => { | ||
for (let i = 0; i < 10_000; i++) { | ||
expect(random.weighted( | ||
['a', 3], | ||
[1, 2] | ||
)).toBeOneOf(['a', 3]) | ||
} | ||
}) | ||
|
||
test('3 pairs of value-weight', () => { | ||
for (let i = 0; i < 10_000; i++) { | ||
expect(random.weighted( | ||
['a', 3, false], | ||
[1, 2, 3] | ||
)).toBeOneOf(['a', 3, false]) | ||
} | ||
}) | ||
}) | ||
}) | ||
}) |