Skip to content
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

Open
GreenImp opened this issue Jul 21, 2020 · 3 comments · May be fixed by #65
Open

Force value to either min or max #42

GreenImp opened this issue Jul 21, 2020 · 3 comments · May be fixed by #65

Comments

@GreenImp
Copy link

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 or real?

e.g.:

// force this to always return the min, 1
integer(1, 4);

// force this to always return the max, 4
integer(1, 4);

I've looked at creating a custom engine, and if the next() method returns 0 it seems to always return the min value:

const engine = {
  next() {
    return 0;
  },
};

integer(1, 4); // returns 1
integer(1, 4); // returns 1
integer(1, 4); // returns 1

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.

@GreenImp
Copy link
Author

GreenImp commented Sep 7, 2020

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 engine

Always 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 engine

Always 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 maxEngine.range before use.

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 .real() but I only needed it to work for .integer().

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).

@rawr51919
Copy link

rawr51919 commented Sep 16, 2023

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
@ckknight would this be a possibility for v3.0.0?

@rawr51919 rawr51919 linked a pull request Sep 19, 2023 that will close this issue
12 tasks
@rawr51919
Copy link

Added in PR #65, will be fixed as soon as it's finished

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 a pull request may close this issue.

2 participants