JavaScript expression to generate a 5-digit number in every case

Christopher Klewes picture Christopher Klewes · Feb 1, 2010 · Viewed 88.6k times · Source

for my selenium tests I need an value provider to get a 5-digit number in every case. The problem with javascript is that the api of Math.random only supports the generation of an 0. starting float. So it has to be between 10000 and 99999.

So it would be easy if it would only generates 0.10000 and higher, but it also generates 0.01000. So this approach doesn't succeed:

Math.floor(Math.random()*100000+1)

Is it possible to generate a 5-digit number in every case (in an expression!) ?

Answer

Rubens Farias picture Rubens Farias · Feb 1, 2010

What about:

Math.floor(Math.random()*90000) + 10000;