Skip to content

Commit

Permalink
refactor(random): Pass min/max as params
Browse files Browse the repository at this point in the history
  • Loading branch information
andreidmt committed Aug 30, 2020
1 parent 9349c82 commit 0912f27
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
13 changes: 5 additions & 8 deletions src/random/random.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
/**
* Generate random number between interval
*
* @param {Object} arg1 Props
* @param {number} arg1.min The minimum
* @param {number} arg1.max The maximum
* @param {Number} min Minimum value
* @param {Number} max Maximum value
*
* @return {integer}
* @return {Integer}
*/
const random = ({ min, max }) =>
Math.floor(Math.random() * (max - min + 1) + min)

export { random }
export const random = (min, max) =>
Math.floor(min+ Math.random() * (max - min + 1))
24 changes: 13 additions & 11 deletions src/random/random.test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import test from "tape"
import chiSquaredTest from "chi-squared-test"

import { random, repeat, get, lt, hist } from ".."
import { random, repeat, get, lt, hist, pipe } from ".."

test("random", t => {
const [min, max, observationCount] = [5, 10, 600]

const observationsHist =
observationCount
|> repeat(() => random({ min, max }))
|> hist
|> Object.values
const observationsHist = pipe(
repeat(() => random(min, max)),
hist,
Object.values
)(observationCount)

const expectedHist =
const expectedHist = repeat(() => observationCount / observationsHist.length)(
observationsHist.length
|> repeat(() => observationCount / observationsHist.length)
)

t.equals(
chiSquaredTest(observationsHist, expectedHist, 1)
|> get("chiSquared")
|> lt(11.07),
pipe(chiSquaredTest, get("chiSquared"), lt(11.07))(
observationsHist,
expectedHist,
1
),
true,
`Generate ${observationCount} integers between ${min} and ${max}}`
)
Expand Down

0 comments on commit 0912f27

Please sign in to comment.