mardi 26 novembre 2019

Generate random number in increments of X in Javascript not working

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.




Aucun commentaire:

Enregistrer un commentaire