aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorinoas <mail@inoas.com>2022-04-13 03:03:59 +0200
committerLouis Pilfold <louis@lpil.uk>2022-04-16 10:23:34 +0100
commit25b4022e833b0c338e8121006d993ad03a778795 (patch)
tree1fc6bb24ec5c054f281ff4038935e604e96efdd9 /src
parent711a7be12ac1a9ee6e5582b390ae92467d6d2d3b (diff)
downloadgleam_stdlib-25b4022e833b0c338e8121006d993ad03a778795.tar.gz
gleam_stdlib-25b4022e833b0c338e8121006d993ad03a778795.zip
allow int.random() to take min and max in any order (2 abitrary boundaries)
Diffstat (limited to 'src')
-rw-r--r--src/gleam/int.gleam7
1 files changed, 6 insertions, 1 deletions
diff --git a/src/gleam/int.gleam b/src/gleam/int.gleam
index 7c6275c..67a3b7a 100644
--- a/src/gleam/int.gleam
+++ b/src/gleam/int.gleam
@@ -397,13 +397,18 @@ fn do_undigits(
}
}
-pub fn random(min: Int, max: Int) -> Int {
+pub fn random(boundary_a: Int, boundary_b: Int) -> Int {
// ```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()