From e950b8de476f7e2ad38eef19179f1505137e9432 Mon Sep 17 00:00:00 2001
From: Neil Fraser
Date: Fri, 8 Dec 2023 23:52:44 +0100
Subject: [PATCH] fix: JS random int block with non-ints.
Previously random_int(2.4, 2.6) would return 2. This is wrong. NaN is better; it's the same as random_int("hello", []).
Python, Lua, PHP all have their own native functions for random int, so we just use their native behaviours. Dart is another matter (separate PR).
I think making this block more accurate is worth the code given that a LOT of kids use "random integer" for stuff.
Also re-flow the Math test blocks, they were overlapping.
And the Dart interpreter link has changed URLs.
---
generators/javascript/math.ts | 8 +++++
tests/generators/golden/generated.js | 8 +++++
tests/generators/index.html | 2 +-
tests/generators/math.xml | 44 ++++++++++++++--------------
4 files changed, 39 insertions(+), 23 deletions(-)
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 {
|
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...
|