File tree 1 file changed +9
-6
lines changed
1 file changed +9
-6
lines changed Original file line number Diff line number Diff line change 2
2
3
3
use rand:: prelude:: * ;
4
4
use rand:: distributions:: uniform:: { SampleUniform , UniformSampler } ;
5
+ use rand:: AsByteSliceMut ;
5
6
6
7
use BigInt ;
7
8
use BigUint ;
@@ -39,13 +40,15 @@ impl<R: Rng + ?Sized> RandBigInt for R {
39
40
fn gen_biguint ( & mut self , bit_size : usize ) -> BigUint {
40
41
use super :: big_digit:: BITS ;
41
42
let ( digits, rem) = bit_size. div_rem ( & BITS ) ;
42
- let mut data = Vec :: with_capacity ( digits + 1 ) ;
43
- for _ in 0 ..digits {
44
- data. push ( self . gen ( ) ) ;
45
- }
43
+ let mut data = vec ! [ BigDigit :: default ( ) ; digits + ( rem > 0 ) as usize ] ;
44
+ // `fill_bytes` is faster than many `gen::<u32>` calls
45
+ self . fill_bytes ( data[ ..] . as_byte_slice_mut ( ) ) ;
46
+ // Swap bytes per the `Rng::fill` source. This might be
47
+ // unnecessary if reproducibility across architectures is not
48
+ // desired.
49
+ data. to_le ( ) ;
46
50
if rem > 0 {
47
- let final_digit: BigDigit = self . gen ( ) ;
48
- data. push ( final_digit >> ( BITS - rem) ) ;
51
+ data[ digits] >>= BITS - rem;
49
52
}
50
53
BigUint :: new ( data)
51
54
}
You can’t perform that action at this time.
0 commit comments