aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorinoas <mail@inoas.com>2022-04-08 22:59:04 +0200
committerLouis Pilfold <louis@lpil.uk>2022-04-16 10:23:34 +0100
commit39764618ea7597cf25c294aea1bb227064cef70e (patch)
tree2cecdfb04af2000a6ae9ecfeec2acc8420aae1c7 /src
parent907c6e367ddc572f60293ecd37c2780b0b2385df (diff)
downloadgleam_stdlib-39764618ea7597cf25c294aea1bb227064cef70e.tar.gz
gleam_stdlib-39764618ea7597cf25c294aea1bb227064cef70e.zip
add random_seed
Diffstat (limited to 'src')
-rw-r--r--src/gleam/float.gleam27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam
index 2e24f0b..385d9b4 100644
--- a/src/gleam/float.gleam
+++ b/src/gleam/float.gleam
@@ -320,3 +320,30 @@ fn do_product(numbers: List(Float), initial: Float) -> Float {
[x, ..rest] -> do_product(rest, x *. initial)
}
}
+
+/// Returns a random seed where 0.0 =< random_seed < 1.0
+///
+pub fn random_seed() -> Float {
+ do_random_seed()
+}
+
+if erlang {
+ // 0.0 =< X < 1.0 and updates the state in the process dictionary.
+ /// Returns a random float uniformly distributed in the value range
+ /// See: <https://www.erlang.org/doc/man/rand.html#uniform-0>
+ ///
+ external fn do_random_seed() -> Float =
+ "rand" "uniform"
+}
+
+if javascript {
+ // 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>
+ ///
+ external fn do_random_seed() -> Float =
+ "math" "random"
+}