diff options
author | inoas <mail@inoas.com> | 2022-04-13 03:00:34 +0200 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2022-04-16 10:23:34 +0100 |
commit | 711a7be12ac1a9ee6e5582b390ae92467d6d2d3b (patch) | |
tree | dec809dcea09e9820485faffdfbcaf4b51e263bf /src | |
parent | 4e06f94b78b3a47ed5b9745e7f646ed261fbe98c (diff) | |
download | gleam_stdlib-711a7be12ac1a9ee6e5582b390ae92467d6d2d3b.tar.gz gleam_stdlib-711a7be12ac1a9ee6e5582b390ae92467d6d2d3b.zip |
remove public random_uniform, rename random_between to random
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/float.gleam | 37 | ||||
-rw-r--r-- | src/gleam/int.gleam | 4 |
2 files changed, 17 insertions, 24 deletions
diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam index 4ff48b2..b04135f 100644 --- a/src/gleam/float.gleam +++ b/src/gleam/float.gleam @@ -336,11 +336,22 @@ fn do_product(numbers: List(Float), initial: Float) -> Float { } } -/// Returns a uniform random number -/// Thus where 0.0 =< value < 1.0 +/// Returns 0.0 if boundary_a and boundary_b are equal +/// Based on: +/// ```javascript +/// return Math.random() * (max - min) + min; // The minimum is inclusive and the maximum is exclusive +/// ``` +/// See: <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random#getting_a_random_number_between_two_values> /// -pub fn random_uniform() -> Float { - do_random_uniform() +pub fn random(boundary_a: Float, boundary_b: Float) -> Float { + let #(min, max) = case boundary_a, boundary_b { + a, b if a <=. b -> #(a, b) + a, b if a >. b -> #(b, a) + } + case min, max { + min, _max if min == max -> min + min, max -> do_random_uniform() *. absolute_difference(min, max) +. min + } } if erlang { @@ -356,21 +367,3 @@ if javascript { external fn do_random_uniform() -> Float = "../gleam_stdlib.mjs" "random_uniform" } - -/// Returns 0.0 if boundary_a and boundary_b are equal -/// Based on: -/// ```javascript -/// return Math.random() * (max - min) + min; // The minimum is inclusive and the maximum is exclusive -/// ``` -/// See: <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random#getting_a_random_number_between_two_values> -/// -pub fn random_between(boundary_a: Float, boundary_b: Float) -> Float { - let #(min, max) = case boundary_a, boundary_b { - a, b if a <=. b -> #(a, b) - a, b if a >. b -> #(b, a) - } - case min, max { - min, _max if min == max -> min - min, max -> random_uniform() *. absolute_difference(min, max) +. min - } -} diff --git a/src/gleam/int.gleam b/src/gleam/int.gleam index 1f7957a..7c6275c 100644 --- a/src/gleam/int.gleam +++ b/src/gleam/int.gleam @@ -397,7 +397,7 @@ fn do_undigits( } } -pub fn random_between(min: Int, max: Int) -> Int { +pub fn random(min: Int, max: Int) -> Int { // ```javascript // min = Math.ceil(min); // max = Math.floor(max); @@ -411,7 +411,7 @@ pub fn random_between(min: Int, max: Int) -> Int { to_float(max) |> float.floor() - float.random_between(min, max) + float.random(min, max) |> float.floor() |> float.round() } |