Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 409
Christopher Klewes Avatar asked Feb 01 '10 08:02

Christopher Klewes


People also ask

How do you find 5 digit numbers are there in all?

As the name says, a 5-digit number compulsorily has 5 digits in it. The smallest 5 digit number is 10,000 and the greatest 5 digit number is 99,999. There are 90,000 five-digit numbers in all. The digit at the ten thousands place in a 5-digit number can never be 0.

How do you generate a random 5 digit number in Java?

Just generate a int value = random. nextInt(100000) so that you will obtain a value in [0,99999] . Now your definition of 5 digits pin is not precise, 40 could be interpreted as 00040 so it's still 5 digits if you pad it.

How do you make a number generator in JavaScript?

Javascript creates pseudo-random numbers with the function Math. random() . This function takes no parameters and creates a random decimal number between 0 and 1. The returned value may be 0, but it will never be 1.


2 Answers

What about:

Math.floor(Math.random()*90000) + 10000; 
like image 81
Rubens Farias Avatar answered Oct 02 '22 22:10

Rubens Farias


Yes, you can create random numbers in any given range:

var min = 10000; var max = 99999; var num = Math.floor(Math.random() * (max - min + 1)) + min; 

Or simplified:

var num = Math.floor(Math.random() * 90000) + 10000; 
like image 42
Guffa Avatar answered Oct 02 '22 23:10

Guffa