samedi 7 juillet 2018

Generate random float between with both min & max inclusive in JavaScript [duplicate]

This question already has an answer here:

Is there a way to generate a number that is inclusive for both min & max for a range of two floats in JavaScript?

Looking at MDN (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random), I know it's possible to create an integer that is inclusive between a range using:

function getRandomIntInclusive(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

and that it's also possible to create a random float with inclusive min & exclusive max using:

function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;
}

but is it possible to create a random float with BOTH inclusive min & max?




Aucun commentaire:

Enregistrer un commentaire