From dc71009b74846164a70814b103d36ff7381cfc82 Mon Sep 17 00:00:00 2001 From: inoas Date: Tue, 12 Apr 2022 01:57:46 +0200 Subject: erlang-bug --- src/gleam/float.gleam | 35 +++++++++++++++++++++++++++++++---- src/gleam/int.gleam | 2 +- 2 files changed, 32 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam index d41bc21..2c7930f 100644 --- a/src/gleam/float.gleam +++ b/src/gleam/float.gleam @@ -342,15 +342,42 @@ if javascript { "../gleam_stdlib.mjs" "random_uniform" } -pub fn random_between(min: Float, max: Float) -> Float { +import gleam/io + +pub fn random_between(boundary_a: Float, boundary_b: Float) -> Float { // ```javascript // return Math.random() * (max - min) + min; // The minimum is inclusive and the maximum is exclusive // ``` // See: // - random_uniform() *. { max -. min } +. min + case boundary_a, boundary_b { + a, b if a <. 0.0 && b == 0.0 -> { + io.debug("a is neg, b is 0.0") + let a = absolute_value(a) + // a1 prohibits variable 'A@1' is unbound {A, B} when (A@1 < 0.0) andalso (B@1 =:= 0.0) -> + let a = absolute_value(b) + random_uniform() *. a + |> negate() + } + a, b if a == 0.0 && b <. 0.0 -> { + io.debug("a 0.0, b is neg") + let a1 = absolute_value(a) + let b1 = absolute_value(b) + random_uniform() *. { a1 -. b1 } +. a1 + |> negate() + } + a, b if a <. b -> { + io.debug("a is smaller than b") + random_uniform() *. { b -. a } +. b + } + a, b if a >. b -> { + io.debug("b is smaller than a") + random_uniform() *. { a -. b } +. a + } + a, b if a == b -> a + } } -pub fn random_below(max: Float) -> Float { - random_uniform() *. max +pub fn random_to(exclusive_boundary: Float) -> Float { + random_uniform() *. exclusive_boundary } diff --git a/src/gleam/int.gleam b/src/gleam/int.gleam index 06ad12c..1a31c0c 100644 --- a/src/gleam/int.gleam +++ b/src/gleam/int.gleam @@ -401,6 +401,6 @@ pub fn random_between(min: Int, max: Int) -> Int { |> float.round() } -pub fn random_below(max: Int) -> Int { +pub fn random_to(max: Int) -> Int { random_between(0, max) } -- cgit v1.2.3