@@ -526,7 +526,7 @@ trait RandBigUInt {
526526 fn gen_biguint ( & mut self , bit_size : uint ) -> BigUint ;
527527}
528528
529- impl < R : RngUtil > RandBigUInt for R {
529+ impl < R : Rng > RandBigUInt for R {
530530 /// Generate a random BigUint of the given bit size.
531531 fn gen_biguint ( & mut self , bit_size : uint ) -> BigUint {
532532 let ( digits, rem) = bit_size. div_rem ( & BigDigit :: bits) ;
@@ -1078,13 +1078,27 @@ trait RandBigInt {
10781078 fn gen_bigint(&mut self, bit_size: uint) -> BigInt;
10791079}
10801080
1081- impl<R: RngUtil > RandBigInt for R {
1081+ impl<R: Rng > RandBigInt for R {
10821082 /// Generate a random BigUint of the given bit size.
10831083 fn gen_bigint(&mut self, bit_size: uint) -> BigInt {
1084+ // Generate a random BigUint...
10841085 let biguint = self.gen_biguint(bit_size);
1085- let sign = if biguint.is_zero() { Zero }
1086- else if self.gen() { Plus }
1087- else { Minus };
1086+ // ...and then randomly assign it a Sign...
1087+ let sign = if biguint.is_zero() {
1088+ // ...except that if the BigUint is zero, we need to try
1089+ // again with probability 0.5. This is because otherwise,
1090+ // the probability of generating a zero BigInt would be
1091+ // double that of any other number.
1092+ if self.gen() {
1093+ return self.gen_bigint(bit_size);
1094+ } else {
1095+ Zero
1096+ }
1097+ } else if self.gen() {
1098+ Plus
1099+ } else {
1100+ Minus
1101+ };
10881102 return BigInt::from_biguint(sign, biguint);
10891103 }
10901104}
@@ -1620,7 +1634,8 @@ mod biguint_tests {
16201634 #[ test]
16211635 fn test_rand ( ) {
16221636 let mut rng = task_rng ( ) ;
1623- let n: BigUint = rng. gen_biguint ( 137 ) ;
1637+ let _n: BigUint = rng. gen_biguint ( 137 ) ;
1638+ assert ! ( rng. gen_biguint( 0 ) . is_zero( ) ) ;
16241639 }
16251640}
16261641
@@ -2056,7 +2071,7 @@ mod bigint_tests {
20562071 #[ test]
20572072 fn test_rand ( ) {
20582073 let mut rng = task_rng ( ) ;
2059- let n : BigInt = rng. gen_bigint ( 137 ) ;
2074+ let _n : BigInt = rng. gen_bigint ( 137 ) ;
20602075 assert ! ( rng. gen_bigint( 0 ) . is_zero( ) ) ;
20612076 }
20622077}
0 commit comments