@@ -387,7 +387,7 @@ pub trait Rng: RngCore {
387387 /// ```
388388 ///
389389 /// [`Uniform`]: distributions/uniform/struct.Uniform.html
390- fn gen_range < T : PartialOrd + SampleUniform > ( & mut self , low : T , high : T ) -> T {
390+ fn gen_range < T : SampleUniform > ( & mut self , low : T , high : T ) -> T {
391391 T :: Sampler :: sample_single ( low, high, self )
392392 }
393393
@@ -509,7 +509,8 @@ pub trait Rng: RngCore {
509509
510510 /// Return a bool with a probability `p` of being true.
511511 ///
512- /// This is a wrapper around [`distributions::Bernoulli`].
512+ /// See also the [`Bernoulli`] distribution, which may be faster if
513+ /// sampling from the same probability repeatedly.
513514 ///
514515 /// # Example
515516 ///
@@ -522,15 +523,44 @@ pub trait Rng: RngCore {
522523 ///
523524 /// # Panics
524525 ///
525- /// If `p` < 0 or `p` > 1.
526+ /// If `p < 0` or `p > 1` .
526527 ///
527- /// [`distributions:: Bernoulli`]: distributions/bernoulli/struct.Bernoulli.html
528+ /// [`Bernoulli`]: distributions/bernoulli/struct.Bernoulli.html
528529 #[ inline]
529530 fn gen_bool ( & mut self , p : f64 ) -> bool {
530531 let d = distributions:: Bernoulli :: new ( p) ;
531532 self . sample ( d)
532533 }
533534
535+ /// Return a bool with a probability of `numerator/denominator` of being
536+ /// true. I.e. `gen_ratio(2, 3)` has chance of 2 in 3, or about 67%, of
537+ /// returning true. If `numerator == denominator`, then the returned value
538+ /// is guaranteed to be `true`. If `numerator == 0`, then the returned
539+ /// value is guaranteed to be `false`.
540+ ///
541+ /// See also the [`Bernoulli`] distribution, which may be faster if
542+ /// sampling from the same `numerator` and `denominator` repeatedly.
543+ ///
544+ /// # Panics
545+ ///
546+ /// If `denominator == 0` or `numerator > denominator`.
547+ ///
548+ /// # Example
549+ ///
550+ /// ```
551+ /// use rand::{thread_rng, Rng};
552+ ///
553+ /// let mut rng = thread_rng();
554+ /// println!("{}", rng.gen_ratio(2, 3));
555+ /// ```
556+ ///
557+ /// [`Bernoulli`]: distributions/bernoulli/struct.Bernoulli.html
558+ #[ inline]
559+ fn gen_ratio ( & mut self , numerator : u32 , denominator : u32 ) -> bool {
560+ let d = distributions:: Bernoulli :: from_ratio ( numerator, denominator) ;
561+ self . sample ( d)
562+ }
563+
534564 /// Return a random element from `values`.
535565 ///
536566 /// Return `None` if `values` is empty.
@@ -1017,4 +1047,21 @@ mod test {
10171047 ( u8 , i8 , u16 , i16 , u32 , i32 , u64 , i64 ) ,
10181048 ( f32 , ( f64 , ( f64 , ) ) ) ) = random ( ) ;
10191049 }
1050+
1051+ #[ test]
1052+ fn test_gen_ratio_average ( ) {
1053+ const NUM : u32 = 3 ;
1054+ const DENOM : u32 = 10 ;
1055+ const N : u32 = 100_000 ;
1056+
1057+ let mut sum: u32 = 0 ;
1058+ let mut rng = rng ( 111 ) ;
1059+ for _ in 0 ..N {
1060+ if rng. gen_ratio ( NUM , DENOM ) {
1061+ sum += 1 ;
1062+ }
1063+ }
1064+ let avg = ( sum as f64 ) / ( N as f64 ) ;
1065+ assert ! ( ( avg - ( NUM as f64 ) /( DENOM as f64 ) ) . abs( ) < 1e-3 ) ;
1066+ }
10201067}
0 commit comments