@@ -526,7 +526,7 @@ trait RandBigUInt {
526
526
fn gen_biguint ( & mut self , bit_size : uint ) -> BigUint ;
527
527
}
528
528
529
- impl < R : RngUtil > RandBigUInt for R {
529
+ impl < R : Rng > RandBigUInt for R {
530
530
/// Generate a random BigUint of the given bit size.
531
531
fn gen_biguint ( & mut self , bit_size : uint ) -> BigUint {
532
532
let ( digits, rem) = bit_size. div_rem ( & BigDigit :: bits) ;
@@ -1078,13 +1078,27 @@ trait RandBigInt {
1078
1078
fn gen_bigint(&mut self, bit_size: uint) -> BigInt;
1079
1079
}
1080
1080
1081
- impl<R: RngUtil > RandBigInt for R {
1081
+ impl<R: Rng > RandBigInt for R {
1082
1082
/// Generate a random BigUint of the given bit size.
1083
1083
fn gen_bigint(&mut self, bit_size: uint) -> BigInt {
1084
+ // Generate a random BigUint...
1084
1085
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
+ };
1088
1102
return BigInt::from_biguint(sign, biguint);
1089
1103
}
1090
1104
}
@@ -1620,7 +1634,8 @@ mod biguint_tests {
1620
1634
#[ test]
1621
1635
fn test_rand ( ) {
1622
1636
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( ) ) ;
1624
1639
}
1625
1640
}
1626
1641
@@ -2056,7 +2071,7 @@ mod bigint_tests {
2056
2071
#[ test]
2057
2072
fn test_rand ( ) {
2058
2073
let mut rng = task_rng ( ) ;
2059
- let n : BigInt = rng. gen_bigint ( 137 ) ;
2074
+ let _n : BigInt = rng. gen_bigint ( 137 ) ;
2060
2075
assert ! ( rng. gen_bigint( 0 ) . is_zero( ) ) ;
2061
2076
}
2062
2077
}
0 commit comments