@@ -521,6 +521,7 @@ pub trait Rng: RngCore {
521521 /// # Example
522522 ///
523523 /// ```rust
524+ /// #[allow(deprecated)]
524525 /// use rand::{thread_rng, Rng};
525526 ///
526527 /// let mut rng = thread_rng();
@@ -532,11 +533,29 @@ pub trait Rng: RngCore {
532533 /// // First meaningful use of `gen_weighted_bool`.
533534 /// println!("{}", rng.gen_weighted_bool(3));
534535 /// ```
536+ #[ deprecated( since="0.5.0" , note="use gen_bool instead" ) ]
535537 fn gen_weighted_bool ( & mut self , n : u32 ) -> bool {
536538 // Short-circuit after `n <= 1` to avoid panic in `gen_range`
537539 n <= 1 || self . gen_range ( 0 , n) == 0
538540 }
539541
542+ /// Return a bool with a probability `p` of being true.
543+ ///
544+ /// # Example
545+ ///
546+ /// ```rust
547+ /// use rand::{thread_rng, Rng};
548+ ///
549+ /// let mut rng = thread_rng();
550+ /// println!("{}", rng.gen_bool(1.0 / 3.0));
551+ /// ```
552+ fn gen_bool ( & mut self , p : f64 ) -> bool {
553+ assert ! ( p >= 0.0 && p <= 1.0 ) ;
554+ // If `p` is constant, this will be evaluated at compile-time.
555+ let p_int = ( p * core:: u32:: MAX as f64 ) as u32 ;
556+ self . gen :: < u32 > ( ) <= p_int
557+ }
558+
540559 /// Return an iterator of random characters from the set A-Z,a-z,0-9.
541560 ///
542561 /// # Example
@@ -798,7 +817,7 @@ impl<R: SeedableRng> NewRng for R {
798817 }
799818}
800819
801- /// The standard RNG. The PRNG algorithm in `StdRng` is choosen to be efficient
820+ /// The standard RNG. The PRNG algorithm in `StdRng` is chosen to be efficient
802821/// on the current platform, to be statistically strong and unpredictable
803822/// (meaning a cryptographically secure PRNG).
804823///
@@ -847,7 +866,7 @@ impl SeedableRng for StdRng {
847866}
848867
849868/// An RNG recommended when small state, cheap initialization and good
850- /// performance are required. The PRNG algorithm in `SmallRng` is choosen to be
869+ /// performance are required. The PRNG algorithm in `SmallRng` is chosen to be
851870/// efficient on the current platform, **without consideration for cryptography
852871/// or security**. The size of its state is much smaller than for `StdRng`.
853872///
@@ -890,10 +909,12 @@ impl SeedableRng for StdRng {
890909pub struct SmallRng ( XorShiftRng ) ;
891910
892911impl RngCore for SmallRng {
912+ #[ inline( always) ]
893913 fn next_u32 ( & mut self ) -> u32 {
894914 self . 0 . next_u32 ( )
895915 }
896916
917+ #[ inline( always) ]
897918 fn next_u64 ( & mut self ) -> u64 {
898919 self . 0 . next_u64 ( )
899920 }
@@ -1076,12 +1097,22 @@ mod test {
10761097 }
10771098
10781099 #[ test]
1100+ #[ allow( deprecated) ]
10791101 fn test_gen_weighted_bool ( ) {
10801102 let mut r = rng ( 104 ) ;
10811103 assert_eq ! ( r. gen_weighted_bool( 0 ) , true ) ;
10821104 assert_eq ! ( r. gen_weighted_bool( 1 ) , true ) ;
10831105 }
10841106
1107+ #[ test]
1108+ fn test_gen_bool ( ) {
1109+ let mut r = rng ( 105 ) ;
1110+ for _ in 0 ..5 {
1111+ assert_eq ! ( r. gen_bool( 0.0 ) , false ) ;
1112+ assert_eq ! ( r. gen_bool( 1.0 ) , true ) ;
1113+ }
1114+ }
1115+
10851116 #[ test]
10861117 fn test_choose ( ) {
10871118 let mut r = rng ( 107 ) ;
0 commit comments