From 39764618ea7597cf25c294aea1bb227064cef70e Mon Sep 17 00:00:00 2001 From: inoas Date: Fri, 8 Apr 2022 22:59:04 +0200 Subject: add random_seed --- src/gleam/float.gleam | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src') diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam index 2e24f0b..385d9b4 100644 --- a/src/gleam/float.gleam +++ b/src/gleam/float.gleam @@ -320,3 +320,30 @@ fn do_product(numbers: List(Float), initial: Float) -> Float { [x, ..rest] -> do_product(rest, x *. initial) } } + +/// Returns a random seed where 0.0 =< random_seed < 1.0 +/// +pub fn random_seed() -> Float { + do_random_seed() +} + +if erlang { + // 0.0 =< X < 1.0 and updates the state in the process dictionary. + /// Returns a random float uniformly distributed in the value range + /// See: + /// + external fn do_random_seed() -> Float = + "rand" "uniform" +} + +if javascript { + // with round-to-nearest-even behavior, the ranges claimed for the functions below + // (excluding the one for Math.random() itself) aren't exact. + // If extremely large bounds are chosen (2^53 or higher), + // it's possible in extremely rare cases to calculate the usually-excluded upper bound. + /// Note that as numbers in JavaScript are IEEE 754 floating point numbers + /// See: + /// + external fn do_random_seed() -> Float = + "math" "random" +} -- cgit v1.2.3