File tree Expand file tree Collapse file tree 3 files changed +13
-3
lines changed Expand file tree Collapse file tree 3 files changed +13
-3
lines changed Original file line number Diff line number Diff line change @@ -87,9 +87,12 @@ STATIC uint32_t yasmarang_randbelow(uint32_t n) {
8787
8888STATIC mp_obj_t mod_urandom_getrandbits (mp_obj_t num_in ) {
8989 int n = mp_obj_get_int (num_in );
90- if (n > 32 || n == 0 ) {
90+ if (n > 32 || n < 0 ) {
9191 mp_raise_ValueError (MP_ERROR_TEXT ("bits must be 32 or less" ));
9292 }
93+ if (n == 0 ) {
94+ return MP_OBJ_NEW_SMALL_INT (0 );
95+ }
9396 uint32_t mask = ~0 ;
9497 // Beware of C undefined behavior when shifting by >= than bit size
9598 mask >>= (32 - n );
Original file line number Diff line number Diff line change 2222random .seed (1 )
2323print (random .getrandbits (16 ) == r )
2424
25- # check that it throws an error for zero bits
25+ # check that zero bits works
26+ print (random .getrandbits (0 ))
27+
28+ # check that it throws an error for negative bits
2629try :
27- random .getrandbits (0 )
30+ random .getrandbits (- 1 )
2831except ValueError :
2932 print ("ValueError" )
Original file line number Diff line number Diff line change 1+ True
2+ True
3+ 0
4+ ValueError
You can’t perform that action at this time.
0 commit comments