From 25b4022e833b0c338e8121006d993ad03a778795 Mon Sep 17 00:00:00 2001 From: inoas Date: Wed, 13 Apr 2022 03:03:59 +0200 Subject: allow int.random() to take min and max in any order (2 abitrary boundaries) --- src/gleam/int.gleam | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src') 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: + 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() -- cgit v1.2.3