Skip to content

Commit 4625d1c

Browse files
committed
---
yaml --- r: 144946 b: refs/heads/try2 c: 54368af h: refs/heads/master v: v3
1 parent 3bf3b42 commit 4625d1c

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 43399529343bff6fc4ee5643b1f8f642b151ffff
8+
refs/heads/try2: 54368afc03054937e7f5a3b7a9b8cf9c8e85d962
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libextra/num/bigint.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)