1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//! Extension functionality for the [`fastrand`] crate.
//!
//! This crate contains code that may be of some use to users of [`fastrand`]. Code contained in
//! this crate is not included in [`fastrand`] due to either the niche not being large enough to
//! justify the new functionality or for semver concerns.
//!
//! ## Usage
//!
//! Various functions are exposed in this crate as top-level functions. These manipulate the global
//! thread-local RNG and can be used without any local state. Note that these require the `"std"`
//! default feature to be enabled.
//!
//! ```
//! # #[cfg(feature = "std")] {
//! use fastrand_contrib::f32_range;
//!
//! let x = f32_range(1.5..3.0);
//! assert!(x >= 1.5 && x < 3.0);
//! # }
//! ```
//!
//! To extend [`fastrand::Rng`], import the [`RngExt`] trait.
//!
//! ```
//! use fastrand_contrib::RngExt;
//! ```
//!
//! Now, all new methods are available on [`fastrand::Rng`].
//!
//! ```
//! use fastrand::Rng;
//! use fastrand_contrib::RngExt;
//!
//! let mut rng = Rng::with_seed(0x1234);
//! let x = rng.f32_range(1.5..3.0);
//! assert!(x >= 1.5 && x < 3.0);
//! ```
//! # Features
//!
//! - `std` (enabled by default): Enables the `std` library. Freestanding functions only work with this
//!   feature enabled. Also enables the `fastrand/std` feature.
//! - `libm`: Uses [`libm`] dependency for math functions in `no_std` environment.
//!
//! Note that some functions are not available in `no_std` context if `libm` feature is not enabled.
//!
//! [`fastrand`]: https://crates.io/crates/fastrand
//! [`fastrand::Rng`]: https://docs.rs/fastrand/latest/fastrand/struct.Rng.html
//! [`libm`]: https://crates.io/crates/libm

#![cfg_attr(not(feature = "std"), no_std)]
#![forbid(unsafe_code, future_incompatible, missing_docs)]
#![doc(
    html_favicon_url = "https://raw.githubusercontent.com/smol-rs/smol/master/assets/images/logo_fullsize_transparent.png"
)]
#![doc(
    html_logo_url = "https://raw.githubusercontent.com/smol-rs/smol/master/assets/images/logo_fullsize_transparent.png"
)]

mod float_normal;
mod float_range;

use core::ops::RangeBounds;

pub use fastrand::{self, Rng};

trait BaseRng {
    fn f32(&mut self) -> f32;
    fn f64(&mut self) -> f64;
    fn bool(&mut self) -> bool;
    fn u128(&mut self) -> u128;
}

impl BaseRng for Rng {
    #[inline]
    fn f32(&mut self) -> f32 {
        Rng::f32(self)
    }
    #[inline]
    fn f64(&mut self) -> f64 {
        Rng::f64(self)
    }
    #[inline]
    fn bool(&mut self) -> bool {
        Rng::bool(self)
    }
    #[inline]
    fn u128(&mut self) -> u128 {
        Rng::u128(self, ..)
    }
}

#[cfg(feature = "std")]
struct GlobalRng;

#[cfg(feature = "std")]
impl BaseRng for GlobalRng {
    #[inline]
    fn f32(&mut self) -> f32 {
        fastrand::f32()
    }
    #[inline]
    fn f64(&mut self) -> f64 {
        fastrand::f64()
    }
    #[inline]
    fn bool(&mut self) -> bool {
        fastrand::bool()
    }
    #[inline]
    fn u128(&mut self) -> u128 {
        fastrand::u128(..)
    }
}

macro_rules! define_ext {
    ($(
        $(#[$meta:meta])*
        fn $name:ident(&mut self, $($argname:ident:$argty:ty),*) -> $ret:ty => $imp:path;
    )*) => {
        /// Extra methods for [`fastrand::Rng`].
        pub trait RngExt {
            $(
            $(#[$meta])*
            fn $name(&mut self, $($argname: $argty),*) -> $ret;
            )*
        }

        impl RngExt for Rng {
            $(
            $(#[$meta])*
            fn $name(&mut self, $($argname: $argty),*) -> $ret {
                $imp(self, $($argname),*)
            }
            )*
        }

        $(
        #[cfg(feature = "std")]
        $(#[$meta])*
        pub fn $name($($argname:$argty),*) -> $ret {
            impl GlobalRng {
                $(#[$meta])*
                fn $name(&mut self, $($argname:$argty),*) -> $ret {
                    $imp(self, $($argname),*)
                }
            }

            GlobalRng::$name(&mut GlobalRng, $($argname),*)
        }
        )*
    }
}

define_ext! {
    /// Generate a 32-bit floating point number in the specified range.
    fn f32_range(&mut self, range: impl RangeBounds<f32>) -> f32 => float_range::f32;

    /// Generate a 64-bit floating point number in the specified range.
    fn f64_range(&mut self, range: impl RangeBounds<f64>) -> f64 => float_range::f64;

    /// Generate a 32-bit floating point number in the normal distribution with
    /// mean mu and standard deviation sigma.
    #[cfg(any(feature = "std", feature = "libm"))]
    fn f32_normal(&mut self, mu: f32, sigma: f32) -> f32 => float_normal::f32;

    /// Generate a 64-bit floating point number in the normal distribution with
    /// mean mu and standard deviation sigma.
    #[cfg(any(feature = "std", feature = "libm"))]
    fn f64_normal(&mut self, mu: f64, sigma: f64) -> f64 => float_normal::f64;

    /// Generate a 32-bit floating point number in the normal distribution with
    /// mean mu and standard deviation sigma using an approximation algorithm.
    fn f32_normal_approx(&mut self, mu: f32, sigma: f32) -> f32 => float_normal::f32_approx;

    /// Generate a 64-bit floating point number in the normal distribution with
    /// mean mu and standard deviation sigma using an approximation algorithm.
    fn f64_normal_approx(&mut self, mu: f64, sigma: f64) -> f64 => float_normal::f64_approx;
}

mod __private {
    #[doc(hidden)]
    pub trait Sealed {}
    impl Sealed for fastrand::Rng {}
}