File tree 2 files changed +7
-43
lines changed
2 files changed +7
-43
lines changed Original file line number Diff line number Diff line change @@ -402,8 +402,8 @@ fn do_product(numbers: List(Float), initial: Float) -> Float {
402
402
}
403
403
}
404
404
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
+ ///
407
407
///
408
408
/// ## Examples
409
409
///
@@ -412,22 +412,8 @@ fn do_product(numbers: List(Float), initial: Float) -> Float {
412
412
/// 2.646355926896028
413
413
/// ```
414
414
///
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
431
417
}
432
418
433
419
/// Returns a random float uniformly distributed in the value range
Original file line number Diff line number Diff line change @@ -514,8 +514,7 @@ fn do_undigits(
514
514
}
515
515
}
516
516
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.
519
518
///
520
519
/// ## Examples
521
520
///
@@ -524,29 +523,8 @@ fn do_undigits(
524
523
/// 2
525
524
/// ```
526
525
///
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 ) )
550
528
|> float . floor ( )
551
529
|> float . round ( )
552
530
}
You can’t perform that action at this time.
0 commit comments