Skip to content

Conversation

@TEParsons
Copy link
Contributor

I noticed some discrepancies between the random functions in utils and their numpy counterparts. The functions themselves seem simple enough that my rudimentary JS should be adequate so I had a go at bringing about parity. Apart from numpy.random.normal - for which I've just added an exception to be a bit more informative.

I've done some testing just from copying the functions to a script and running it in an HTML file, but please do run it through the proper test suite! My tests were:

console.log([random(), random(), random(), random(), random(),])
console.log(random(5))
console.log([randint(4), randint(4), randint(4), randint(4), randint(4),])
console.log(randint(5))
console.log(randint(2, 4, 5))
console.log(shuffle([1, 2, 3]))
console.log(shuffle([1, 2, 3]))
console.log(shuffle([1, 2, 3]))
console.log(randchoice([1, 2, 3], 3, false, [0.5, 0.1, 0.1]))
console.log(randchoice([1, 2, 3], 3, false, [0.5, 0.1, 0.1]))
console.log(randchoice([1, 2, 3], 3, false, [0.5, 0.1, 0.1]))
console.log(randchoice([1, 2, 3], 5, true, [0.5, 0.1, 0.1]))

which seemed to behave as expected

* @param {number} [size = 1] - number of values to return
* @returns {number} a random integer in the requested range (signed)
*/
export function randint(min, max = null, size = 1)
Copy link

@shun2wang shun2wang Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should also raise errors if given non-integer negative range parameters like this:
randint(-10.1, 1) will return also a non-integer.

Otherwise everything looks good to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants