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

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

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 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!