0

I'm trying to generate random numbers in increments of X using a generic function in Javascript which returns a random number, my function is:

function getRndInteger(min, max) {
  return Math.floor(Math.random() * (max - min + 1) ) * 30 + min;
}

When used, e.g: getRndInteger(50, 500) a random number will be generated, e.g: 253, however, I'm trying to modify my function to generate a random number between Z and X, but in increments of A, so for instance, 250, 300, 350, 400 ... etc. I've tried:

function getRndInteger(min, max) {
  return Math.floor(Math.random() * (max - min + 1) ) * 50 + min;
}

// and...

function getRndInteger(min, max) {
  return Math.floor(Math.random() * (max - min + 1) * 50 ) + min;
}

both of these return numbers beyond my maximum number.

5 Answers 5

1

You could divide by the wanted delta and multiply the random value by it to get a wanted gap.

function getRndInteger(min, max, delta) {
    return delta * Math.floor(Math.random() * (max - min) / delta + min / delta);
}

console.log(...Array.from({ length: 10 }, getRndInteger.bind(null, 50, 500, 50)));

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

function getRndInteger(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min;
}
var x = 1;
var max = 1000;
for(i=0;i<=5;i++){
  x = getRndInteger(x,max)
  console.log(x);
}

Comments

0

I'm not sure to have understand well. This function generate numbers from min and max with a specific step:

function getRndInteger(min, max, step) {
  const range = (max - min) / step;
  return Math.floor(Math.random() * range) * step + min  
}
console.log(getRndInteger(50, 500, 10))

is it what you need?

Comments

0

As described on the Mozilla Developer Site, use this approach:

/**
 * Returns a random number between min (inclusive) and max (exclusive)
 */
function getRandomArbitrary(min, max) {
    return Math.random() * (max - min) + min;
}

/**
 * Returns a random integer between min (inclusive) and max (inclusive).
 * The value is no lower than min (or the next integer greater than min
 * if min isn't an integer) and no greater than max (or the next integer
 * lower than max if max isn't an integer).
 * Using Math.round() will give you a non-uniform distribution!
 */
function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

After you have a random number in your range, you can then round it to the desired increment.

Math.ceil(number / 100) * 100 // rounds to next 100

Comments

0
function random(min, max) {
  // get max multiplier 
  let totalSteps = Math.floor(max / min);

  // get a random multiplier between 1 and max multiplier
  let randomStep = Math.floor(Math.random() * totalSteps) + 1;

  // multiply by min and return
  return randomStep * min;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.