From 75802b6408842e4f59521bd52e72237ef31cd64e Mon Sep 17 00:00:00 2001 From: inoas Date: Mon, 11 Apr 2022 23:54:33 +0200 Subject: wip --- src/gleam/float.gleam | 21 +++++++++++++++------ src/gleam/int.gleam | 28 +++++++++++++++++++++++++++- src/gleam_stdlib.mjs | 2 +- 3 files changed, 43 insertions(+), 8 deletions(-) (limited to 'src') 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: /// - 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: /// - 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: + 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: + 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: + 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(); } -- cgit v1.2.3