Skip to content

Commit a5519c3

Browse files
committed
Simplify random functions
1 parent a0d3028 commit a5519c3

File tree

2 files changed

+7
-43
lines changed

2 files changed

+7
-43
lines changed

src/gleam/float.gleam

+4-18
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,8 @@ fn do_product(numbers: List(Float), initial: Float) -> Float {
402402
}
403403
}
404404

405-
/// Returns `0.0` if `boundary_a` and `boundary_b` are equal,
406-
/// otherwise returns a `Float x` where `lower_boundary =< x < upper_boundary`.
405+
/// Generates a random float between the given minimum and maximum values.
406+
///
407407
///
408408
/// ## Examples
409409
///
@@ -412,22 +412,8 @@ fn do_product(numbers: List(Float), initial: Float) -> Float {
412412
/// 2.646355926896028
413413
/// ```
414414
///
415-
pub fn random(boundary_a: Float, boundary_b: Float) -> Float {
416-
// Based on:
417-
//
418-
// ```javascript
419-
// return Math.random() * (max - min) + min; // The minimum is inclusive and the maximum is exclusive
420-
// ```
421-
//
422-
// See: <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random#getting_a_random_number_between_two_values>
423-
let #(min, max) = case boundary_a, boundary_b {
424-
a, b if a <=. b -> #(a, b)
425-
a, b if a >. b -> #(b, a)
426-
}
427-
case min, max {
428-
min, _max if min == max -> min
429-
min, max -> do_random_uniform() *. { max -. min } +. min
430-
}
415+
pub fn random(min: Float, max: Float) -> Float {
416+
do_random_uniform() *. { max -. min } +. min
431417
}
432418

433419
/// Returns a random float uniformly distributed in the value range

src/gleam/int.gleam

+3-25
Original file line numberDiff line numberDiff line change
@@ -514,8 +514,7 @@ fn do_undigits(
514514
}
515515
}
516516

517-
/// Returns `0` if `boundary_a` and `boundary_b` are equal,
518-
/// otherwise returns an `Int x` where `lower_boundary =< x < upper_boundary`.
517+
/// Generates a random int between the given minimum and maximum values.
519518
///
520519
/// ## Examples
521520
///
@@ -524,29 +523,8 @@ fn do_undigits(
524523
/// 2
525524
/// ```
526525
///
527-
pub fn random(boundary_a: Int, boundary_b: Int) -> Int {
528-
// Based on:
529-
//
530-
// ```javascript
531-
// min = Math.ceil(min);
532-
// max = Math.floor(max);
533-
// return Math.floor(Math.random() * (max - min) + min); // The minimum is inclusive and the maximum is exclusive
534-
// ```
535-
//
536-
// See: <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random#getting_a_random_integer_between_two_values>
537-
let #(min, max) = case boundary_a, boundary_b {
538-
a, b if a <= b -> #(a, b)
539-
a, b if a > b -> #(b, a)
540-
}
541-
542-
let min =
543-
to_float(min)
544-
|> float.ceiling()
545-
let max =
546-
to_float(max)
547-
|> float.floor()
548-
549-
float.random(min, max)
526+
pub fn random(min: Int, max: Int) -> Int {
527+
float.random(to_float(min), to_float(max))
550528
|> float.floor()
551529
|> float.round()
552530
}

0 commit comments

Comments
 (0)