Javascript / html: How to create a random number between number A and number B?

We have a form with two fields and a button. Do we want a random number int (for example, 3, 5 or 33) that would lie between int A and int B? (without using jQuery or anything like that)

+3
source share
3 answers

You can use Javascript Math.random

function randomInRange(start,end){
       return Math.floor(Math.random() * (end - start + 1) + start);
}
+6
source

Use something like Math.floor(Math.random()*(intB-intA +1)) + intA?

+3
source

:

Math.floor(a + Math.random() * (b - a))

Math.random() [0,1) - 0 () ) 1 ().

+1
source

Source: https://habr.com/ru/post/1782693/


All Articles