diff options
-rw-r--r-- | src/gleam/float.gleam | 14 | ||||
-rw-r--r-- | src/gleam_stdlib.mjs | 4 | ||||
-rw-r--r-- | test/gleam/float_test.gleam | 12 |
3 files changed, 17 insertions, 13 deletions
diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam index 385d9b4..4c314a9 100644 --- a/src/gleam/float.gleam +++ b/src/gleam/float.gleam @@ -321,18 +321,18 @@ fn do_product(numbers: List(Float), initial: Float) -> Float { } } -/// Returns a random seed where 0.0 =< random_seed < 1.0 +/// Returns a random value where 0.0 =< value < 1.0 /// -pub fn random_seed() -> Float { - do_random_seed() +pub fn random() -> Float { + do_random() } if erlang { + // Returns a random float uniformly distributed in the value range // 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 = + external fn do_random() -> Float = "rand" "uniform" } @@ -344,6 +344,6 @@ if javascript { /// 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" + external fn do_random() -> Float = + "../gleam_stdlib.mjs" "random" } diff --git a/src/gleam_stdlib.mjs b/src/gleam_stdlib.mjs index 4dd4aa7..5d80640 100644 --- a/src/gleam_stdlib.mjs +++ b/src/gleam_stdlib.mjs @@ -246,6 +246,10 @@ export function power(base, exponent) { return Math.pow(base, exponent); } +export function random() { + return Math.random(); +} + export function bit_string_slice(bits, position, length) { let start = Math.min(position, position + length); let end = Math.max(position, position + length); diff --git a/test/gleam/float_test.gleam b/test/gleam/float_test.gleam index 109f728..dcb9ed2 100644 --- a/test/gleam/float_test.gleam +++ b/test/gleam/float_test.gleam @@ -301,12 +301,12 @@ pub fn product_test() { |> should.equal(33.6) } -pub fn random_seed_test() { - let random_seed = float.random_seed() +pub fn random_test() { + let random = float.random() - { random_seed >=. 0.0 } - |> should.be_true + { random >=. 0. } + |> should.be_true() - { random_seed <. 1.0 } - |> should.be_true + { random <. 1. } + |> should.be_true() } |