aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorinoas <mail@inoas.com>2022-04-13 03:00:34 +0200
committerLouis Pilfold <louis@lpil.uk>2022-04-16 10:23:34 +0100
commit711a7be12ac1a9ee6e5582b390ae92467d6d2d3b (patch)
treedec809dcea09e9820485faffdfbcaf4b51e263bf
parent4e06f94b78b3a47ed5b9745e7f646ed261fbe98c (diff)
downloadgleam_stdlib-711a7be12ac1a9ee6e5582b390ae92467d6d2d3b.tar.gz
gleam_stdlib-711a7be12ac1a9ee6e5582b390ae92467d6d2d3b.zip
remove public random_uniform, rename random_between to random
-rw-r--r--src/gleam/float.gleam37
-rw-r--r--src/gleam/int.gleam4
-rw-r--r--test/gleam/float_test.gleam39
-rw-r--r--test/gleam/int_test.gleam12
4 files changed, 32 insertions, 60 deletions
diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam
index 4ff48b2..b04135f 100644
--- a/src/gleam/float.gleam
+++ b/src/gleam/float.gleam
@@ -336,11 +336,22 @@ fn do_product(numbers: List(Float), initial: Float) -> Float {
}
}
-/// Returns a uniform random number
-/// Thus where 0.0 =< value < 1.0
+/// Returns 0.0 if boundary_a and boundary_b are equal
+/// 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>
///
-pub fn random_uniform() -> Float {
- do_random_uniform()
+pub fn random(boundary_a: Float, boundary_b: Float) -> Float {
+ 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() *. absolute_difference(min, max) +. min
+ }
}
if erlang {
@@ -356,21 +367,3 @@ if javascript {
external fn do_random_uniform() -> Float =
"../gleam_stdlib.mjs" "random_uniform"
}
-
-/// Returns 0.0 if boundary_a and boundary_b are equal
-/// 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>
-///
-pub fn random_between(boundary_a: Float, boundary_b: Float) -> Float {
- 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 -> random_uniform() *. absolute_difference(min, max) +. min
- }
-}
diff --git a/src/gleam/int.gleam b/src/gleam/int.gleam
index 1f7957a..7c6275c 100644
--- a/src/gleam/int.gleam
+++ b/src/gleam/int.gleam
@@ -397,7 +397,7 @@ fn do_undigits(
}
}
-pub fn random_between(min: Int, max: Int) -> Int {
+pub fn random(min: Int, max: Int) -> Int {
// ```javascript
// min = Math.ceil(min);
// max = Math.floor(max);
@@ -411,7 +411,7 @@ pub fn random_between(min: Int, max: Int) -> Int {
to_float(max)
|> float.floor()
- float.random_between(min, max)
+ float.random(min, max)
|> float.floor()
|> float.round()
}
diff --git a/test/gleam/float_test.gleam b/test/gleam/float_test.gleam
index 126f27d..faf7d7c 100644
--- a/test/gleam/float_test.gleam
+++ b/test/gleam/float_test.gleam
@@ -330,53 +330,32 @@ pub fn product_test() {
|> should.equal(33.6)
}
-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()
-
- Nil
- }
- list.range(0, 100)
- |> iterator.from_list
- |> iterator.fold(Nil, one_random_uniform_test_set)
-}
-
-pub fn random_between_test() {
+pub fn random_test() {
let test_boundaries = fn(_acc, _e) {
- float.random_between(0.0, 0.0)
+ float.random(0.0, 0.0)
|> should.equal(0.0)
- float.random_between(0.0, 10.0)
+ float.random(0.0, 10.0)
|> fn(x) { x >=. 0.0 && x <. 10.0 }
|> should.be_true
- float.random_between(10.0, 0.0)
+ float.random(10.0, 0.0)
|> fn(x) { x >=. 0.0 && x <. 10.0 }
|> should.be_true
- float.random_between(0.0, -10.0)
+ float.random(0.0, -10.0)
|> fn(x) { x >=. -10.0 && x <. 0.0 }
|> should.be_true
- float.random_between(-10.0, 0.0)
+ float.random(-10.0, 0.0)
|> fn(x) { x >=. -10.0 && x <. 0.0 }
|> should.be_true
- float.random_between(-10.0, 10.0)
+ float.random(-10.0, 10.0)
|> fn(x) { x >=. -10.0 && x <. 10.0 }
|> should.be_true
- float.random_between(10.0, -10.0)
+ float.random(10.0, -10.0)
|> fn(x) { x >=. -10.0 && x <. 10.0 }
|> should.be_true
}
@@ -395,7 +374,7 @@ pub fn random_between_test() {
|> iterator.from_list()
|> iterator.fold(
from: 0.0,
- with: fn(acc, _element) { acc +. float.random_between(min, max) },
+ with: fn(acc, _element) { acc +. float.random(min, max) },
)
|> fn(sum) { sum /. int.to_float(iterations) }
|> float.loosely_compare(expected_average, tolerance)
diff --git a/test/gleam/int_test.gleam b/test/gleam/int_test.gleam
index 79c444b..9129c66 100644
--- a/test/gleam/int_test.gleam
+++ b/test/gleam/int_test.gleam
@@ -346,20 +346,20 @@ pub fn undigits_test() {
|> should.equal(Error(int.InvalidBase))
}
-pub fn random_between_test() {
+pub fn random_test() {
let test_boundaries = fn(_acc, _e) {
- int.random_between(0, 0)
+ int.random(0, 0)
|> should.equal(0)
- int.random_between(-1, 0)
+ int.random(-1, 0)
|> list.contains([-1, 0], _)
|> should.be_true
- int.random_between(-1, 1)
+ int.random(-1, 1)
|> list.contains([-1, 0], _)
|> should.be_true
- int.random_between(-1, 2)
+ int.random(-1, 2)
|> list.contains([-1, 0, 1], _)
|> should.be_true
}
@@ -373,7 +373,7 @@ pub fn random_between_test() {
|> iterator.from_list
|> iterator.fold(
from: 0,
- with: fn(acc, _element) { acc + int.random_between(min, max) },
+ with: fn(acc, _element) { acc + int.random(min, max) },
)
|> fn(sum) { sum / iterations }
|> fn(sum) {