aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorinoas <mail@inoas.com>2022-04-11 23:54:33 +0200
committerLouis Pilfold <louis@lpil.uk>2022-04-16 10:23:34 +0100
commit75802b6408842e4f59521bd52e72237ef31cd64e (patch)
treebd252fffdd9816fed3e155d27925a8d62b5d1585 /src
parentdb20f5a296038fa6ba73172eac5bd5a3818942da (diff)
downloadgleam_stdlib-75802b6408842e4f59521bd52e72237ef31cd64e.tar.gz
gleam_stdlib-75802b6408842e4f59521bd52e72237ef31cd64e.zip
wip
Diffstat (limited to 'src')
-rw-r--r--src/gleam/float.gleam21
-rw-r--r--src/gleam/int.gleam28
-rw-r--r--src/gleam_stdlib.mjs2
3 files changed, 43 insertions, 8 deletions
diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam
index 4c314a9..20fd6c8 100644
--- a/src/gleam/float.gleam
+++ b/src/gleam/float.gleam
@@ -321,10 +321,11 @@ fn do_product(numbers: List(Float), initial: Float) -> Float {
}
}
-/// Returns a random value where 0.0 =< value < 1.0
+/// Returns a uniform random number
+/// Thus where 0.0 =< value < 1.0
///
-pub fn random() -> Float {
- do_random()
+pub fn random_uniform() -> Float {
+ do_random_uniform()
}
if erlang {
@@ -332,7 +333,7 @@ if erlang {
// 0.0 =< X < 1.0 and updates the state in the process dictionary.
/// See: <https://www.erlang.org/doc/man/rand.html#uniform-0>
///
- external fn do_random() -> Float =
+ external fn do_random_uniform() -> Float =
"rand" "uniform"
}
@@ -344,6 +345,14 @@ if javascript {
/// Note that as numbers in JavaScript are IEEE 754 floating point numbers
/// See: <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random>
///
- external fn do_random() -> Float =
- "../gleam_stdlib.mjs" "random"
+ external fn do_random_uniform() -> Float =
+ "../gleam_stdlib.mjs" "random_uniform"
+}
+
+pub fn random_between(min: Float, max: Float) -> Float {
+ // ```javascript
+ // return Math.floor(Math.random() * (max - min + 1) + min); // The maximum is/shuould-be 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
}
diff --git a/src/gleam/int.gleam b/src/gleam/int.gleam
index c60affb..9da5c97 100644
--- a/src/gleam/int.gleam
+++ b/src/gleam/int.gleam
@@ -1,4 +1,6 @@
import gleam/order.{Order}
+// import gleam/float.{floor, parse, random}
+import gleam/float
/// Returns the absolute value of the input.
///
@@ -324,7 +326,7 @@ fn do_product(numbers: List(Int), initial: Int) -> Int {
}
}
-/// Splits an integer into its digit representation in the specified base
+/// Splits an integer into its digit representation in the specified base
///
/// ## Examples
///
@@ -380,3 +382,27 @@ fn do_undigits(
[digit, ..rest] -> do_undigits(rest, base, acc * base + digit)
}
}
+
+pub fn random_below(max: Int) -> Int {
+ // ```javascript
+ // return Math.floor(rand * max);
+ // ```
+ // See: <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random>
+ float.random_uniform() *. to_float(max)
+ |> float.floor()
+ |> float.round()
+ // Does `float.round() `affect random distribution uniformity?
+}
+
+pub fn random_between(min: Int, max: Int) -> Int {
+ // ```javascript
+ // min = Math.ceil(min);
+ // max = Math.floor(max);
+ // return Math.floor(Math.random() * (max - min + 1) + min); //The maximum is
+ // ```
+ // See: <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random#getting_a_random_integer_between_two_values>
+ float.random_between(to_float(min), to_float(max))
+ |> float.floor()
+ |> float.round()
+ // Does float.round() affect random distribution uniformity?
+}
diff --git a/src/gleam_stdlib.mjs b/src/gleam_stdlib.mjs
index 5d80640..59077e9 100644
--- a/src/gleam_stdlib.mjs
+++ b/src/gleam_stdlib.mjs
@@ -246,7 +246,7 @@ export function power(base, exponent) {
return Math.pow(base, exponent);
}
-export function random() {
+export function random_uniform() {
return Math.random();
}