This repository was archived by the owner on Nov 15, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +15
-1
lines changed Expand file tree Collapse file tree 1 file changed +15
-1
lines changed Original file line number Diff line number Diff line change @@ -27,6 +27,8 @@ use crate::traits::{Hash, TrailingZeroInput};
27
27
///
28
28
/// It can be saved and later reloaded using the Codec traits.
29
29
///
30
+ /// (It is recommended to use the `rand_chacha` crate as an alternative to this where possible.)
31
+ ///
30
32
/// Example:
31
33
/// ```
32
34
/// use sp_runtime::traits::{Hash, BlakeTwo256};
@@ -63,7 +65,7 @@ impl<Hashing: Hash> RandomNumberGenerator<Hashing> {
63
65
/// Returns a number at least zero, at most `max`.
64
66
pub fn pick_u32 ( & mut self , max : u32 ) -> u32 {
65
67
let needed = ( 4 - max. leading_zeros ( ) / 8 ) as usize ;
66
- let top = ( ( 1 << ( needed as u64 * 8 ) ) / ( ( max + 1 ) as u64 ) * ( ( max + 1 ) as u64 ) - 1 ) as u32 ;
68
+ let top = ( ( 1 << ( needed as u64 * 8 ) ) / ( max as u64 + 1 ) * ( max as u64 + 1 ) - 1 ) as u32 ;
67
69
loop {
68
70
if self . offset ( ) + needed > self . current . as_ref ( ) . len ( ) {
69
71
// rehash
@@ -102,3 +104,15 @@ impl<Hashing: Hash> RandomNumberGenerator<Hashing> {
102
104
}
103
105
}
104
106
}
107
+
108
+ #[ cfg( test) ]
109
+ mod tests {
110
+ use super :: RandomNumberGenerator ;
111
+ use crate :: traits:: { Hash , BlakeTwo256 } ;
112
+
113
+ #[ test]
114
+ fn does_not_panic_on_max ( ) {
115
+ let seed = BlakeTwo256 :: hash ( b"Fourty-two" ) ;
116
+ let _random = RandomNumberGenerator :: < BlakeTwo256 > :: new ( seed) . pick_u32 ( u32:: MAX ) ;
117
+ }
118
+ }
You can’t perform that action at this time.
0 commit comments