diff options
-rw-r--r-- | src/gleam/float.gleam | 35 | ||||
-rw-r--r-- | src/gleam/int.gleam | 2 | ||||
-rw-r--r-- | test/gleam/float_test.gleam | 57 | ||||
-rw-r--r-- | test/gleam/int_test.gleam | 96 |
4 files changed, 131 insertions, 59 deletions
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: <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random#getting_a_random_number_between_two_values> // - 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) } diff --git a/test/gleam/float_test.gleam b/test/gleam/float_test.gleam index 6c87f65..39fa9a8 100644 --- a/test/gleam/float_test.gleam +++ b/test/gleam/float_test.gleam @@ -3,6 +3,8 @@ import gleam/float import gleam/order import gleam/list import gleam/iterator +import gleam/function +import gleam/io pub fn parse_test() { "1.23" @@ -307,11 +309,13 @@ pub fn random_uniform_test() { let one_random_uniform_test_set = fn(_acc, _e) { { float.random_uniform() >=. 0. } |> should.be_true() + { float.random_uniform() <. 0. } |> should.be_false() { float.random_uniform() <. 1. } |> should.be_true() + { float.random_uniform() >=. 1. } |> should.be_false() @@ -321,3 +325,56 @@ pub fn random_uniform_test() { |> iterator.from_list |> iterator.fold(Nil, one_random_uniform_test_set) } + +pub fn random_between_test() { + // let one_random_between_test_set = fn(_acc, _e) { + // float.random_between(0.0, 0.0) + // |> should.equal(0.0) + // } + float.random_between(-1.0, 0.0) + |> function.tap(io.debug) + // |> function.tap(fn(x) { + // fn(x) { x >=. -1.0 }(x) + // |> should.be_true + // }) + |> function.tap(fn(x) { + fn(x) { x <. 0.0 }(x) + |> should.be_true + }) + // float.random_between(0.0, -1.0) + // |> fn(x) { x >=. -1.0 && x <. 0.0 } + // |> should.be_true + // float.random_between(-1.0, 1.0) + // |> fn(x) { x >=. -1.0 && x <. 1.0 } + // |> should.be_true + // float.random_between(1.0, -1.0) + // |> fn(x) { x >=. -1.0 && x <. 1.0 } + // |> should.be_true + // float.random_between(-1.0, 2.0) + // |> fn(x) { x >=. -1.0 && x <. 2.0 } + // |> should.be_true + // float.random_between(2.0, -1.0) + // |> fn(x) { x >=. -1.0 && x <. 2.0 } + // |> should.be_true + // list.range(0, 1) + // |> iterator.from_list + // |> iterator.fold(Nil, one_random_between_test_set) +} +// pub fn random_to_test() { +// let one_random_to_test_set = fn(_acc, _e) { +// float.random_to(0.0) +// |> should.equal(0.0) +// float.random_to(-1.0) +// |> fn(x) { x >=. -1.0 && x <. 0.0 } +// |> should.be_true +// float.random_to(1.0) +// |> fn(x) { x >=. 0.0 && x <. 1.0 } +// |> should.be_true +// float.random_to(2.0) +// |> fn(x) { x >=. 0.0 && x <. 2.0 } +// |> should.be_true +// } +// list.range(0, 100) +// |> iterator.from_list +// |> iterator.fold(Nil, one_random_to_test_set) +// } diff --git a/test/gleam/int_test.gleam b/test/gleam/int_test.gleam index 203eb37..b7a425b 100644 --- a/test/gleam/int_test.gleam +++ b/test/gleam/int_test.gleam @@ -318,57 +318,45 @@ pub fn undigits_test() { int.undigits([1, 1, 2], 2) |> should.equal(Error(int.InvalidBase)) } - -pub fn random_between_test() { - let one_random_between_test_set = fn(_acc, _e) { - int.random_between(0, 0) - |> should.equal(0) - - int.random_between(-1, 0) - |> list.contains([-1, 0], _) - |> should.be_true - - int.random_between(-1, 1) - |> list.contains([-1, 0], _) - |> should.be_true - - int.random_between(-1, 2) - |> list.contains([-1, 0, 1], _) - |> should.be_true - } - - list.range(0, 100) - |> iterator.from_list - |> iterator.fold(Nil, one_random_between_test_set) -} - -pub fn random_below_test() { - let one_random_below_test_set = fn(_acc, _e) { - int.random_below(0) - |> should.equal(0) - - int.random_below(-1) - |> list.contains([-1], _) - |> should.be_true - - int.random_below(1) - |> list.contains([0], _) - |> should.be_true - - int.random_below(2) - |> list.contains([0, 1], _) - |> should.be_true - - int.random_below(3) - |> list.contains([0, 1, 2], _) - |> should.be_true - - int.random_below(4) - |> list.contains([0, 1, 2, 3], _) - |> should.be_true - } - - list.range(0, 100) - |> iterator.from_list - |> iterator.fold(Nil, one_random_below_test_set) -} +// pub fn random_between_test() { +// let one_random_between_test_set = fn(_acc, _e) { +// int.random_between(0, 0) +// |> should.equal(0) +// int.random_between(-1, 0) +// |> list.contains([-1, 0], _) +// |> should.be_true +// int.random_between(-1, 1) +// |> list.contains([-1, 0], _) +// |> should.be_true +// int.random_between(-1, 2) +// |> list.contains([-1, 0, 1], _) +// |> should.be_true +// } +// list.range(0, 100) +// |> iterator.from_list +// |> iterator.fold(Nil, one_random_between_test_set) +// } +// pub fn random_to_test() { +// let one_random_to_test_set = fn(_acc, _e) { +// int.random_to(0) +// |> should.equal(0) +// int.random_to(-1) +// |> list.contains([-1], _) +// |> should.be_true +// int.random_to(1) +// |> list.contains([0], _) +// |> should.be_true +// int.random_to(2) +// |> list.contains([0, 1], _) +// |> should.be_true +// int.random_to(3) +// |> list.contains([0, 1, 2], _) +// |> should.be_true +// int.random_to(4) +// |> list.contains([0, 1, 2, 3], _) +// |> should.be_true +// } +// list.range(0, 100) +// |> iterator.from_list +// |> iterator.fold(Nil, one_random_to_test_set) +// } |