aboutsummaryrefslogtreecommitdiff
path: root/src/gleam_stdlib.mjs
diff options
context:
space:
mode:
authorinoas <mail@inoas.com>2022-04-12 00:43:17 +0200
committerLouis Pilfold <louis@lpil.uk>2022-04-16 10:23:34 +0100
commitac2cbe7e3937106d485d4cb763ddf6e90085f2e1 (patch)
tree3d0669d2ccc1c38ebe04e4d5cf66de7d9e86a9ea /src/gleam_stdlib.mjs
parent1217a1fe1a13cecc347251456f5beb972474cb80 (diff)
downloadgleam_stdlib-ac2cbe7e3937106d485d4cb763ddf6e90085f2e1.tar.gz
gleam_stdlib-ac2cbe7e3937106d485d4cb763ddf6e90085f2e1.zip
fixes and refactoring
Diffstat (limited to 'src/gleam_stdlib.mjs')
-rw-r--r--src/gleam_stdlib.mjs13
1 files changed, 12 insertions, 1 deletions
diff --git a/src/gleam_stdlib.mjs b/src/gleam_stdlib.mjs
index 59077e9..6604bff 100644
--- a/src/gleam_stdlib.mjs
+++ b/src/gleam_stdlib.mjs
@@ -247,7 +247,18 @@ export function power(base, exponent) {
}
export function random_uniform() {
- return Math.random();
+ let random_uniform_result = Math.random();
+ // With round-to-nearest-even behavior, the ranges claimed for the functions below
+ // (excluding the one for Math.random() itself) aren't exact.
+ // If extremely large bounds are chosen (2^53 or higher),
+ // it's possible in extremely rare cases to calculate the usually-excluded upper bound.
+ // Note that as numbers in JavaScript are IEEE 754 floating point numbers
+ // See: <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random>
+ // Because of this, we just loop 'until' we get a valid result where 0.0 <= x < 1.0:
+ if (random_uniform_result === 1.0) {
+ return random_uniform();
+ }
+ return random_uniform_result;
}
export function bit_string_slice(bits, position, length) {