How to generate random number between min and max in JavaScript

1 Answer

0 votes
function randomMinMax(min, max) { 
    return Math.floor(Math.random() * (max - min + 1) + min)
}
 
const min = 6
const max = 18
 
for (let i = 0; i < 20; i++)
    console.log(randomMinMax(min, max));
 
 
 
 
 
/*
 
run:
 
10
14
15
6
18
17
18
17
6
11
14
7
15
6
15
14
11
18
16
7
 
*/

 



answered Oct 22, 2021 by avibootz
...