# 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
``406function randomBetween(a, b)407{408    if (a == b)409     return a;410    411    return a + Math.floor(Math.random()*(b+1));412}``
``109function randomInt(a, b)110{111    assert (112        isInt(a) &amp;&amp; isInt(b) &amp;&amp; a &lt;= b,113        'invalid params to randomInt'114    );115116    var range = b - a;117118    var rnd = a + Math.floor(Math.random() * (range + 1));119120    return rnd;121}``
``10function math_random_int(a, b) {11	if (a &gt; 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) + min1617  if (round) {18    return Math.floor(num)19  }20  return num21}``
``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}``