@@ -626,68 +626,7 @@ pub trait Rng: RngCore {
626
626
/// println!("{:?}", y);
627
627
/// ```
628
628
fn shuffle < T > ( & mut self , values : & mut [ T ] ) {
629
- use distributions:: range:: WideningMultiply ;
630
- // In theory this function is nothing more then:
631
- // ```
632
- // while i >= 2 {
633
- // i -= 1;
634
- // values.swap(i, self.gen_range(0, i + 1));`
635
- // }
636
- // ```
637
- //
638
- // `gen_range` samples completely unbiased from the range. It can be 4x
639
- // as fast if only we could sample with a tiny bias. Using a modulus to
640
- // reduce the range will always show a bias towards the lower numbers
641
- // in the range.
642
- //
643
- // But we use the widening multiply method. Here the bias is evenly
644
- // distributed over all numbers in the range. Also for every
645
- // iteration the range is 1 less then the previous iteration, and
646
- // the numbers that might have a bias are different. So besides the
647
- // biases being very tiny, they also partly cancel out each other.
648
- //
649
- // Said differently, we only have to worry about biases when a whole
650
- // group together shows a bias, as in the modulus method.
651
-
652
- // We also optimize for slices of different length: huge slices with
653
- // more than 2^32 elements (usize indexes), large slices with more than
654
- // 2^16 elements, and medium to small slices. This allows us to make
655
- // better use of the bits generated by the RNG.
656
- let mut i = values. len ( ) ;
657
- while i > core:: u32:: MAX as usize || i & 1 == 1 {
658
- // Invariant: elements with index >= i have been locked in place.
659
- i -= 1 ;
660
- // Lock element i in place, by swapping it with a random element in
661
- // the range [0, i] (inclusive).
662
- let ( j, _) = self . gen :: < u64 > ( ) . wmul ( ( i + 1 ) as u64 ) ;
663
- values. swap ( i, j as usize ) ;
664
- }
665
- let mut i = i as u32 ;
666
-
667
- while i > core:: u16:: MAX as u32 || i & 2 == 2 {
668
- let r = self . gen :: < u64 > ( ) ;
669
- let r1 = r as u32 ;
670
- let r2 = ( r >> 32 ) as u32 ;
671
-
672
- i -= 1 ;
673
- let ( j, _) = r1. wmul ( i + 1 ) ;
674
- values. swap ( i as usize , j as usize ) ;
675
-
676
- i -= 1 ;
677
- let ( j, _) = r2. wmul ( i + 1 ) ;
678
- values. swap ( i as usize , j as usize ) ;
679
- }
680
-
681
- let mut i = i as u16 ;
682
- while i >= 2 {
683
- let mut r = self . gen :: < u64 > ( ) ;
684
- for _ in 0 ..4 {
685
- i -= 1 ;
686
- let ( j, _) = ( r as u16 ) . wmul ( i + 1 ) ;
687
- values. swap ( i as usize , j as usize ) ;
688
- r = r >> 16 ;
689
- }
690
- }
629
+ seq:: shuffle ( self , values)
691
630
}
692
631
}
693
632
0 commit comments