aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/gleam/float.gleam37
-rw-r--r--src/gleam/int.gleam4
2 files changed, 17 insertions, 24 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()
}