Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

201
Vistas
im in confusion about this logic, can someone explain me this min and max

var = randNum;

function getRandomNum(min, max){ return Math.floor(Math.random() * (max- min + 1)+ min)

randNum = getRandomNum(1 , 6);

console.log(randNum);

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Let's breakit down We need to find random number in range (min, max)

Math.random() returns a random number between 0 and 1(excluded) Ex (0.1, 0. 245, ... so on) Since our range starts from min Hence we need to add min to the random number min + Math.random()

But this will allow you random values only from min to min + 1

To make sure that we cover the entire range from min to max, we multiply the difference between those numbers max - min

This is good, but we only need Integer numbers, hence we round it to floor value using Math.floor And to account for that exclude 1 from Math.random() we add it to the value

Hence it becomes Math.floor(Math.random() * (max- min + 1)+ min)

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda