10 examples of 'javascript random between two numbers' in JavaScript

Every line of 'javascript random between two numbers' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
406function randomBetween(a, b)
407{
408 if (a == b)
409 return a;
410
411 return a + Math.floor(Math.random()*(b+1));
412}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

109function randomInt(a, b)
110{
111 assert (
112 isInt(a) && isInt(b) && a <= b,
113 'invalid params to randomInt'
114 );
115
116 var range = b - a;
117
118 var rnd = a + Math.floor(Math.random() * (range + 1));
119
120 return rnd;
121}
10function math_random_int(a, b) {
11 if (a > b) {
12 // Swap a and b to ensure a is smaller.
13 var c = a;
14 a = b;
15 b = c;
16 }
17 return Math.floor(Math.random() * (b - a + 1) + a);
18}
51function random(from, to) {
52 return Math.floor(Math.random() * (to - from + 1) + from);
53}
437function randomBetween(min, max) {
438 return Math.floor(Math.random() * (max - min + 1) + min);
439}
66function randomBetween(min, max) {
67 return Math.random() * (max - min + 1) + min;
68};
576function between(randNumMin, randNumMax)
577{
578 var randInt = Math.floor((Math.random() * ((randNumMax + 1) - randNumMin)) + randNumMin);
579
580 return randInt;
581}
14function randomBetween(min, max, round) {
15 const num = Math.random() * (max - min + 1) + min
16
17 if (round) {
18 return Math.floor(num)
19 }
20 return num
21}
542function RandomBetween(a, b) {
543 return a + (Math.random() * (b - a));
544}
5export function randomBetween(min, max){
6 return Math.floor(Math.random() * (max - min + 1) + min);
7}

Related snippets