File tree Expand file tree Collapse file tree 2 files changed +36
-6
lines changed Expand file tree Collapse file tree 2 files changed +36
-6
lines changed Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ num-integer = "0.1"
23
23
num-traits = " 0.2"
24
24
ndarray = " >= 0.15, < 0.17"
25
25
pyo3 = { version = " 0.25.0" , default-features = false , features = [" macros" ] }
26
+ rand = { version = " 0.9.1" , default-features = false , optional = true }
26
27
rustc-hash = " 2.0"
27
28
28
29
[dev-dependencies ]
Original file line number Diff line number Diff line change @@ -77,18 +77,47 @@ impl<'py> BitGen<'py> {
77
77
}
78
78
}
79
79
80
+ #[ cfg( feature = "rand" ) ]
81
+ impl rand:: RngCore for BitGen < ' _ > {
82
+ fn next_u32 ( & mut self ) -> u32 {
83
+ self . next_uint32 ( )
84
+ }
85
+ fn next_u64 ( & mut self ) -> u64 {
86
+ self . next_uint64 ( )
87
+ }
88
+ fn fill_bytes ( & mut self , dst : & mut [ u8 ] ) {
89
+ rand:: rand_core:: impls:: fill_bytes_via_next ( self , dst)
90
+ }
91
+ }
92
+
80
93
#[ cfg( test) ]
81
94
mod tests {
82
95
use super :: * ;
83
96
97
+ fn get_bit_generator < ' py > ( py : Python < ' py > ) -> PyResult < Bound < ' py , BitGenerator > > {
98
+ let default_rng = py. import ( "numpy.random" ) ?. getattr ( "default_rng" ) ?;
99
+ let bit_generator = default_rng. call0 ( ) ?. getattr ( "bit_generator" ) ?. downcast_into :: < BitGenerator > ( ) ?;
100
+ Ok ( bit_generator)
101
+ }
102
+
84
103
#[ test]
85
- fn test_bitgen ( ) -> PyResult < ( ) > {
104
+ fn bitgen ( ) -> PyResult < ( ) > {
86
105
Python :: with_gil ( |py| {
87
- let default_rng = py. import ( "numpy.random" ) ?. getattr ( "default_rng" ) ?;
88
- let bitgen = default_rng. call0 ( ) ?. getattr ( "bit_generator" ) ?. downcast_into :: < BitGenerator > ( ) ?. bit_gen ( ) ?;
89
- let res = bitgen. next_raw ( ) ;
90
- dbg ! ( res) ;
106
+ let bitgen = get_bit_generator ( py) ?. bit_gen ( ) ?;
107
+ let _ = bitgen. next_raw ( ) ;
91
108
Ok ( ( ) )
92
109
} )
93
- }
110
+ }
111
+
112
+ #[ cfg( feature = "rand" ) ]
113
+ #[ test]
114
+ fn rand ( ) -> PyResult < ( ) > {
115
+ use rand:: Rng as _;
116
+
117
+ Python :: with_gil ( |py| {
118
+ let mut bitgen = get_bit_generator ( py) ?. bit_gen ( ) ?;
119
+ let _ = bitgen. random_ratio ( 2 , 3 ) ;
120
+ Ok ( ( ) )
121
+ } )
122
+ }
94
123
}
You can’t perform that action at this time.
0 commit comments