-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Force value to either min or max #42
Comments
I know this is an odd request, so it's unlikely anyone else wants to achieve this but, just in case, this was my solution; Min engineAlways returns the lowest possible value. /**
* Engine that always returns the minimum value.
*
* @type {{next(): number}}
*/
const minEngine = {
/**
* Returns the minimum number index, `0`
*
* @returns {number}
*/
next() {
return 0;
},
}; E.g. const random = new Random(minEngine);
random.integer(1, 4); // returns 1
random.integer(10, 300); // returns 10
random.integer(-55, -12); // returns -55 Max engineAlways returns the maximum value. /**
* Engine that always returns the maximum value.
*
* @type {{next(): number, range: number[]}}
*/
const maxEngine = {
/**
* The min / max number range (e.g. `[1, 10]`).
*
* This _must_ be set for the `next()` method to return the correct last index.
*
* @example
* maxEngine.range = [1, 10];
*
* @type {number[]}
*/
range: [],
/**
* Returns the maximum number index for the range
*
* @returns {number}
*/
next() {
// calculate the index of the max number
return this.range[1] - this.range[0];
},
}; The max and minimum values must be set against the const random = new Random(maxEngine);
maxEngine.range = [1, 4];
random.integer(1, 4); // returns 4
maxEngine.range = [10, 300];
random.integer(10, 300); // returns 300
maxEngine.range = [-55, -12];
random.integer(-55, -12); // returns -12 It might seem a bit pointless, if I'm having to provide the min and max range, but I'm using it where I'm providing the data programatically, where I don't know what the values are in advance. I'm not sure if this works with I would be interested to know if there's a better way of achieving this (e.g. if random-js can provide the max index so I don't need to calculate it, or just the min/max values so they don't need to be specified manually). |
It would probably make more sense to make a max/min function that hooks into the random.integer/random.die/random.real and takes the entire call as an argument |
Added in PR #65, will be fixed as soon as it's finished |
I know this might be an odd question for a random library but, is there any way of forcing an engine to return the min or max value when using
integer
orreal
?e.g.:
I've looked at creating a custom engine, and if the
next()
method returns0
it seems to always return the min value:I'm assuming it works by returning the number from the range, by index of the response from
next()
. But I can't find a way of forcing the max value.The text was updated successfully, but these errors were encountered: