aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorinoas <mail@inoas.com>2022-04-12 01:57:46 +0200
committerLouis Pilfold <louis@lpil.uk>2022-04-16 10:23:34 +0100
commitdc71009b74846164a70814b103d36ff7381cfc82 (patch)
treeda42505702dfa1bfbff656e3b12085e698daa32e /src
parentaa77e878fd9a9e6289d0f41f0c5156c00e54a8a3 (diff)
downloadgleam_stdlib-dc71009b74846164a70814b103d36ff7381cfc82.tar.gz
gleam_stdlib-dc71009b74846164a70814b103d36ff7381cfc82.zip
erlang-bug
Diffstat (limited to 'src')
-rw-r--r--src/gleam/float.gleam35
-rw-r--r--src/gleam/int.gleam2
2 files changed, 32 insertions, 5 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)
}