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

269
Vistas
Random Numbers In Range Not Random

I've been trying to generate random numbers between 1 and 10 million, but was finding that none of the numbers generated were lower than 1 million. I wrote this code to test the problem. This code selects ten million random numbers between 1 and ten million and outputs the lowest number. I'd expect to get at least something in the 100-1000 range at least, but it;s always at least 1 million. What have I missed?

JSFiddle: https://jsfiddle.net/vhLoqz8g/

var lowest=10000000;
for (let i = 0; i < 10000000; i++) {
  var g=Math.floor(Math.random() * (10000000 - 1 + 1)) + 1;
  if (g<lowest)
  {
  lowest=g;
  }
}

document.write(g);

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You should output lowest and not g at the end of your loop:

var lowest=1e8;
for (let i = 0; i < 1e7; i++) {
  var g=Math.floor(Math.random()* 1e7) + 1;
  if (g<lowest) lowest=g;
}

console.log(lowest)

about 4 years ago · Juan Pablo Isaza 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