En lugar de generar un número aleatorio entre cero y un número dado como hicimos antes, podemos generar un número aleatorio que se encuentre dentro de un rango de dos números específicos.
Para hacer esto, definiremos un número mínimo <code>min</code> y un máximo número <code>max</code> .
Aquí está la fórmula que usaremos. Tómese un momento para leerlo e intente entender lo que hace este código:
<code>Math.floor(Math.random() * (max - min + 1)) + min</code>
Cree una función llamada <code>randomRange</code> que tome un rango <code>myMin</code> y <code>myMax</code> y devuelva un número aleatorio que sea mayor o igual que <code>myMin</code> , y que sea menor o igual que <code>myMax</code> , inclusive.
testString: 'assert(calcMin === 5, "The lowest random number that can be generated by <code>randomRange</code> should be equal to your minimum number, <code>myMin</code>.");'
- text: "El número aleatorio más alto que puede ser generado por <code>randomRange</code> debería ser igual a tu número máximo, <code>myMax</code>".
testString: 'assert(calcMax === 15, "The highest random number that can be generated by <code>randomRange</code> should be equal to your maximum number, <code>myMax</code>.");'
testString: 'assert((function(){if(code.match(/myMax/g).length > 1 && code.match(/myMin/g).length > 2 && code.match(/Math.floor/g) && code.match(/Math.random/g)){return true;}else{return false;}})(), "<code>randomRange</code> should use both <code>myMax</code> and <code>myMin</code>, and return a random number in your range.");'