aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2023-08-19 16:16:38 +0100
committerLouis Pilfold <louis@lpil.uk>2023-08-19 16:16:38 +0100
commita5519c33086b3467039dd6d7178da95630d1a68b (patch)
tree762433d01a7e2209c1495bdc5242725c23f7b2e5
parenta0d30280a91f1d4ce1115536ac7b85ffc6ef49ed (diff)
downloadgleam_stdlib-a5519c33086b3467039dd6d7178da95630d1a68b.tar.gz
gleam_stdlib-a5519c33086b3467039dd6d7178da95630d1a68b.zip
Simplify random functions
-rw-r--r--src/gleam/float.gleam22
-rw-r--r--src/gleam/int.gleam28
2 files changed, 7 insertions, 43 deletions
diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam
index a70a2e9..c8c6147 100644
--- a/src/gleam/float.gleam
+++ b/src/gleam/float.gleam
@@ -402,8 +402,8 @@ fn do_product(numbers: List(Float), initial: Float) -> Float {
}
}
-/// Returns `0.0` if `boundary_a` and `boundary_b` are equal,
-/// otherwise returns a `Float x` where `lower_boundary =< x < upper_boundary`.
+/// Generates a random float between the given minimum and maximum values.
+///
///
/// ## Examples
///
@@ -412,22 +412,8 @@ fn do_product(numbers: List(Float), initial: Float) -> Float {
/// 2.646355926896028
/// ```
///
-pub fn random(boundary_a: Float, boundary_b: Float) -> Float {
- // 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>
- 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() *. { max -. min } +. min
- }
+pub fn random(min: Float, max: Float) -> Float {
+ do_random_uniform() *. { max -. min } +. min
}
/// Returns a random float uniformly distributed in the value range
diff --git a/src/gleam/int.gleam b/src/gleam/int.gleam
index c49df37..c0e650e 100644
--- a/src/gleam/int.gleam
+++ b/src/gleam/int.gleam
@@ -514,8 +514,7 @@ fn do_undigits(
}
}
-/// Returns `0` if `boundary_a` and `boundary_b` are equal,
-/// otherwise returns an `Int x` where `lower_boundary =< x < upper_boundary`.
+/// Generates a random int between the given minimum and maximum values.
///
/// ## Examples
///
@@ -524,29 +523,8 @@ fn do_undigits(
/// 2
/// ```
///
-pub fn random(boundary_a: Int, boundary_b: Int) -> Int {
- // Based on:
- //
- // ```javascript
- // min = Math.ceil(min);
- // max = Math.floor(max);
- // return Math.floor(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_integer_between_two_values>
- let #(min, max) = case boundary_a, boundary_b {
- a, b if a <= b -> #(a, b)
- a, b if a > b -> #(b, a)
- }
-
- let min =
- to_float(min)
- |> float.ceiling()
- let max =
- to_float(max)
- |> float.floor()
-
- float.random(min, max)
+pub fn random(min: Int, max: Int) -> Int {
+ float.random(to_float(min), to_float(max))
|> float.floor()
|> float.round()
}