diff --git a/generators/javascript/math.ts b/generators/javascript/math.ts index e8ab2852f..172d81ff2 100644 --- a/generators/javascript/math.ts +++ b/generators/javascript/math.ts @@ -414,12 +414,20 @@ export function math_random_int( 'mathRandomInt', ` function ${generator.FUNCTION_NAME_PLACEHOLDER_}(a, b) { + a = Number(a); + b = Number(b); if (a > b) { // Swap a and b to ensure a is smaller. var c = a; a = b; b = c; } + a = Math.ceil(a); + b = Math.floor(b); + if (a > b) { + // No integers between the inputs (e.g. 2.4 & 2.6). + return NaN; + } return Math.floor(Math.random() * (b - a + 1) + a); } `, diff --git a/tests/generators/golden/generated.js b/tests/generators/golden/generated.js index f8ac5651a..85c46ef50 100644 --- a/tests/generators/golden/generated.js +++ b/tests/generators/golden/generated.js @@ -560,12 +560,20 @@ function test_constraint() { } function mathRandomInt(a, b) { + a = Number(a); + b = Number(b); if (a > b) { // Swap a and b to ensure a is smaller. var c = a; a = b; b = c; } + a = Math.ceil(a); + b = Math.floor(b); + if (a > b) { + // No integers between the inputs (e.g. 2.4 & 2.6). + return NaN; + } return Math.floor(Math.random() * (b - a + 1) + a); } diff --git a/tests/generators/index.html b/tests/generators/index.html index cb30c0966..c1a73a8ba 100644 --- a/tests/generators/index.html +++ b/tests/generators/index.html @@ -403,7 +403,7 @@ h1 {
-
interpreter +
interpreter

diff --git a/tests/generators/math.xml b/tests/generators/math.xml index 682d143ac..c45929d5b 100644 --- a/tests/generators/math.xml +++ b/tests/generators/math.xml @@ -1,7 +1,7 @@ - varToChange - rand + varToChange + rand Math @@ -71,7 +71,7 @@ - + test single Tests the "single" block. @@ -1255,7 +1255,7 @@ - + test round Tests the "round" block. @@ -1331,12 +1331,12 @@ - + test change Tests the "change" block. - varToChange + varToChange 100 @@ -1344,7 +1344,7 @@ - varToChange + varToChange 42 @@ -1359,7 +1359,7 @@ - varToChange + varToChange @@ -1374,7 +1374,7 @@ - + test operations on list Tests the "list operation" blocks. @@ -1799,7 +1799,7 @@ - + test mod Tests the "mod" block. @@ -1831,7 +1831,7 @@ - + test constraint Tests the "constrain" block. @@ -1868,12 +1868,12 @@ - + test random integer Tests the "random integer" block. - rand + rand @@ -1904,7 +1904,7 @@ GTE - rand + rand @@ -1919,7 +1919,7 @@ LTE - rand + rand @@ -1945,7 +1945,7 @@ WHOLE - rand + rand @@ -1957,12 +1957,12 @@ - + test random fraction Tests the "random fraction" block. - rand + rand @@ -1982,7 +1982,7 @@ GTE - rand + rand @@ -1997,7 +1997,7 @@ LTE - rand + rand @@ -2014,9 +2014,9 @@ - + test atan2 - Describe this function... + Describe this function...