Skip to content

Commit

Permalink
feat(grandom): add shuffle and weighted packages to grandom
Browse files Browse the repository at this point in the history
  • Loading branch information
richrdkng committed Aug 23, 2023
1 parent 3f1caa3 commit 522ed02
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 17 deletions.
38 changes: 30 additions & 8 deletions packages/grandom/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions packages/grandom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"start": "npm test -- --watchAll",
"types": "tsc --noEmit && tsc -p tsconfig.types.json --outDir .temp",
"lint": "ts-standard --verbose | snazzy",
"lint:fix": "ts-standard --verbose --fix | snazzy",
"test": "jest",
"prep": "npm run __prep:init && npm run __prep:copy",
"build": "npm run __build:src && npm run __build:types",
Expand All @@ -42,7 +43,7 @@
"@semantic-release/git": "^10.0.1",
"@testyard/stats": "^1.4.1",
"@tsconfig/node16": "^16.1.1",
"@types/jest": "^29.5.3",
"@types/jest": "^29.5.4",
"@types/node": "^16.18.38",
"@types/semantic-release": "^20.0.1",
"glob": "^10.3.3",
Expand All @@ -67,6 +68,7 @@
"@grandom/number": "^1.4.2",
"@grandom/pick": "^1.3.2",
"@grandom/shuffle": "^1.0.0",
"@grandom/string": "^1.2.2"
"@grandom/string": "^1.2.2",
"@grandom/weighted": "^1.0.0"
}
}
4 changes: 4 additions & 0 deletions packages/grandom/src/Grandom/BasicGrandom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import RandomNumber from '@grandom/number/RandomNumber'
import RandomPick from '@grandom/pick/RandomPick'
import RandomShuffle from '@grandom/shuffle/RandomShuffle'
import RandomString from '@grandom/string/RandomString'
import RandomWeighted from '@grandom/weighted/RandomWeighted'

export default class BasicGrandom {
constructor () {
Expand All @@ -17,13 +18,15 @@ export default class BasicGrandom {
const randomPick = new RandomPick(randomEngine)
const randomShuffle = new RandomShuffle(randomEngine)
const randomString = new RandomString(randomEngine)
const randomWeighted = new RandomWeighted(randomEngine)

this.bigint = randomBigInt.bigint.bind(randomBigInt)
this.boolean = randomBoolean.boolean.bind(randomBoolean)
this.number = randomNumber.number
this.pick = randomPick.pick.bind(randomPick)
this.shuffle = randomShuffle.shuffle.bind(randomShuffle)
this.string = randomString.string.bind(randomString)
this.weighted = randomWeighted.weighted.bind(randomWeighted)
}

bigint: InstanceType<typeof RandomBigInt>['bigint']
Expand All @@ -32,4 +35,5 @@ export default class BasicGrandom {
pick: InstanceType<typeof RandomPick>['pick']
shuffle: InstanceType<typeof RandomShuffle>['shuffle']
string: InstanceType<typeof RandomString>['string']
weighted: InstanceType<typeof RandomWeighted>['weighted']
}
4 changes: 4 additions & 0 deletions packages/grandom/src/Grandom/SeedableGrandom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import RandomNumber from '@grandom/number/RandomNumber'
import RandomPick from '@grandom/pick/RandomPick'
import RandomShuffle from '@grandom/shuffle/RandomShuffle'
import RandomString from '@grandom/string/RandomString'
import RandomWeighted from '@grandom/weighted/RandomWeighted'

export default class SeedableGrandom {
private readonly _engine: MT19937Engine
Expand All @@ -20,13 +21,15 @@ export default class SeedableGrandom {
const randomPick = new RandomPick(this._engine)
const randomShuffle = new RandomShuffle(this._engine)
const randomString = new RandomString(this._engine)
const randomWeighted = new RandomWeighted(this._engine)

this.bigint = randomBigInt.bigint.bind(randomBigInt)
this.boolean = randomBoolean.boolean.bind(randomBoolean)
this.number = randomNumber.number
this.pick = randomPick.pick.bind(randomPick)
this.shuffle = randomShuffle.shuffle.bind(randomShuffle)
this.string = randomString.string.bind(randomString)
this.weighted = randomWeighted.weighted.bind(randomWeighted)
}

get seed (): any {
Expand All @@ -43,4 +46,5 @@ export default class SeedableGrandom {
pick: InstanceType<typeof RandomPick>['pick']
shuffle: InstanceType<typeof RandomShuffle>['shuffle']
string: InstanceType<typeof RandomString>['string']
weighted: InstanceType<typeof RandomWeighted>['weighted']
}
2 changes: 2 additions & 0 deletions packages/grandom/src/shuffle/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import shuffle from '@grandom/shuffle'
export default shuffle
2 changes: 2 additions & 0 deletions packages/grandom/src/weighted/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import weighted from '@grandom/weighted'
export default weighted
22 changes: 22 additions & 0 deletions packages/grandom/test/Grandom/BasicGrandom/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import BasicGrandom from '../../../src/Grandom/BasicGrandom'

describe('Grandom', () => {
test('default usage', () => {
const grandom = new BasicGrandom()

expect(grandom.bigint).toBeFunction()
expect(typeof grandom.bigint()).toBe('bigint')

expect(grandom.boolean).toBeFunction()
expect(typeof grandom.boolean()).toBe('boolean')

expect(grandom.number).toBeFunction()
expect(typeof grandom.number()).toBe('number')

expect(grandom.pick).toBeFunction()
expect(typeof grandom.pick('123')).toBe('string')

expect(grandom.string).toBeFunction()
expect(typeof grandom.string()).toBe('string')
})
})
49 changes: 42 additions & 7 deletions packages/grandom/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,47 @@
import { LENGTH, BooleanStats } from '@testyard/stats'
import grandom from '../src'

describe('grandom', () => {
test('basics', () => {
expect(grandom.bigint).toBeFunction()
expect(grandom.boolean).toBeFunction()
expect(grandom.number).toBeFunction()
expect(grandom.string).toBeFunction()
expect(grandom.shuffle).toBeFunction()
expect(grandom.pick).toBeFunction()
describe('default Grandom instance', () => {
describe('.boolean()', () => {
test('basic usage', () => {
expect(grandom.boolean()).toBeBoolean()
})

describe('biased usage', () => {
test('~50% true / ~50% false', () => {
const { add, result } = new BooleanStats()

for (let i = 0; i < LENGTH; i++) {
add(grandom.boolean())
}

expect(result.true.percent).toBeWithin(0.49, 0.52)
expect(result.false.percent).toBeWithin(0.49, 0.52)
})

test('always true', () => {
const { add, result } = new BooleanStats()

for (let i = 0; i < LENGTH; i++) {
add(grandom.boolean(1))
}

expect(result.true.percent).toBe(1)
expect(result.false.percent).toBe(0)
})

test('always false', () => {
const { add, result } = new BooleanStats()

for (let i = 0; i < LENGTH; i++) {
add(grandom.boolean(0))
}

expect(result.true.percent).toBe(0)
expect(result.false.percent).toBe(1)
})
})
})
})
})

0 comments on commit 522ed02

Please sign in to comment.