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

160
Vistas
Generate a random number with fixed digit without leading zero

I'm using this one liner to generate a random 18 digit number :

Math.floor(100000000000000000 + Math.random() * 900000000000000000)

The problem is that it always contains one or more zeros at the end, and I don't understand why.

How can I generate a really random number that always contains 18 digits, and without a leading zero?

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

0

You have too many digits, you're running against the limit to precision in a javascript number.

about 4 years ago · Juan Pablo Isaza Denunciar

0

It looks like a Problem with the max number Range. You can hack it like this. And it is a better Random number.

Math.floor(10000000000000000 + Math.random() * 90000000000000000)+ "" + Math.floor(Math.random()* 100) 

But if you parse to int you have the same problem.

about 4 years ago · Juan Pablo Isaza Denunciar

0

The solution for this so far is to generate two numbers (10 digits + 8 digits) & concatenate them as follow:

Math.floor(1000000000 + Math.random() * 9000000000) + "" + Math.floor(10000000 + Math.random() * 90000000)
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