Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

165
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!