@@ -67,7 +67,7 @@ STATIC uint32_t yasmarang(void) {
67
67
68
68
// End of Yasmarang
69
69
70
- #if MICROPY_PY_URANDOM_EXTRA_FUNCS
70
+ #if MICROPY_PY_RANDOM_EXTRA_FUNCS
71
71
72
72
// returns an unsigned integer below the given argument
73
73
// n must not be zero
@@ -85,7 +85,7 @@ STATIC uint32_t yasmarang_randbelow(uint32_t n) {
85
85
86
86
#endif
87
87
88
- STATIC mp_obj_t mod_urandom_getrandbits (mp_obj_t num_in ) {
88
+ STATIC mp_obj_t mod_random_getrandbits (mp_obj_t num_in ) {
89
89
int n = mp_obj_get_int (num_in );
90
90
if (n > 32 || n < 0 ) {
91
91
mp_raise_ValueError (MP_ERROR_TEXT ("bits must be 32 or less" ));
@@ -98,13 +98,13 @@ STATIC mp_obj_t mod_urandom_getrandbits(mp_obj_t num_in) {
98
98
mask >>= (32 - n );
99
99
return mp_obj_new_int_from_uint (yasmarang () & mask );
100
100
}
101
- STATIC MP_DEFINE_CONST_FUN_OBJ_1 (mod_urandom_getrandbits_obj , mod_urandom_getrandbits );
101
+ STATIC MP_DEFINE_CONST_FUN_OBJ_1 (mod_random_getrandbits_obj , mod_random_getrandbits );
102
102
103
- STATIC mp_obj_t mod_urandom_seed (size_t n_args , const mp_obj_t * args ) {
103
+ STATIC mp_obj_t mod_random_seed (size_t n_args , const mp_obj_t * args ) {
104
104
mp_uint_t seed ;
105
105
if (n_args == 0 || args [0 ] == mp_const_none ) {
106
- #ifdef MICROPY_PY_URANDOM_SEED_INIT_FUNC
107
- seed = MICROPY_PY_URANDOM_SEED_INIT_FUNC ;
106
+ #ifdef MICROPY_PY_RANDOM_SEED_INIT_FUNC
107
+ seed = MICROPY_PY_RANDOM_SEED_INIT_FUNC ;
108
108
#else
109
109
mp_raise_ValueError (MP_ERROR_TEXT ("no default seed" ));
110
110
#endif
@@ -117,11 +117,11 @@ STATIC mp_obj_t mod_urandom_seed(size_t n_args, const mp_obj_t *args) {
117
117
yasmarang_dat = 0 ;
118
118
return mp_const_none ;
119
119
}
120
- STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (mod_urandom_seed_obj , 0 , 1 , mod_urandom_seed );
120
+ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (mod_random_seed_obj , 0 , 1 , mod_random_seed );
121
121
122
- #if MICROPY_PY_URANDOM_EXTRA_FUNCS
122
+ #if MICROPY_PY_RANDOM_EXTRA_FUNCS
123
123
124
- STATIC mp_obj_t mod_urandom_randrange (size_t n_args , const mp_obj_t * args ) {
124
+ STATIC mp_obj_t mod_random_randrange (size_t n_args , const mp_obj_t * args ) {
125
125
mp_int_t start = mp_obj_get_int (args [0 ]);
126
126
if (n_args == 1 ) {
127
127
// range(stop)
@@ -161,9 +161,9 @@ STATIC mp_obj_t mod_urandom_randrange(size_t n_args, const mp_obj_t *args) {
161
161
error :
162
162
mp_raise_ValueError (NULL );
163
163
}
164
- STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (mod_urandom_randrange_obj , 1 , 3 , mod_urandom_randrange );
164
+ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (mod_random_randrange_obj , 1 , 3 , mod_random_randrange );
165
165
166
- STATIC mp_obj_t mod_urandom_randint (mp_obj_t a_in , mp_obj_t b_in ) {
166
+ STATIC mp_obj_t mod_random_randint (mp_obj_t a_in , mp_obj_t b_in ) {
167
167
mp_int_t a = mp_obj_get_int (a_in );
168
168
mp_int_t b = mp_obj_get_int (b_in );
169
169
if (a <= b ) {
@@ -172,17 +172,17 @@ STATIC mp_obj_t mod_urandom_randint(mp_obj_t a_in, mp_obj_t b_in) {
172
172
mp_raise_ValueError (NULL );
173
173
}
174
174
}
175
- STATIC MP_DEFINE_CONST_FUN_OBJ_2 (mod_urandom_randint_obj , mod_urandom_randint );
175
+ STATIC MP_DEFINE_CONST_FUN_OBJ_2 (mod_random_randint_obj , mod_random_randint );
176
176
177
- STATIC mp_obj_t mod_urandom_choice (mp_obj_t seq ) {
177
+ STATIC mp_obj_t mod_random_choice (mp_obj_t seq ) {
178
178
mp_int_t len = mp_obj_get_int (mp_obj_len (seq ));
179
179
if (len > 0 ) {
180
180
return mp_obj_subscr (seq , mp_obj_new_int (yasmarang_randbelow (len )), MP_OBJ_SENTINEL );
181
181
} else {
182
182
mp_raise_type (& mp_type_IndexError );
183
183
}
184
184
}
185
- STATIC MP_DEFINE_CONST_FUN_OBJ_1 (mod_urandom_choice_obj , mod_urandom_choice );
185
+ STATIC MP_DEFINE_CONST_FUN_OBJ_1 (mod_random_choice_obj , mod_random_choice );
186
186
187
187
#if MICROPY_PY_BUILTINS_FLOAT
188
188
@@ -199,63 +199,63 @@ STATIC mp_float_t yasmarang_float(void) {
199
199
return u .f - 1 ;
200
200
}
201
201
202
- STATIC mp_obj_t mod_urandom_random (void ) {
202
+ STATIC mp_obj_t mod_random_random (void ) {
203
203
return mp_obj_new_float (yasmarang_float ());
204
204
}
205
- STATIC MP_DEFINE_CONST_FUN_OBJ_0 (mod_urandom_random_obj , mod_urandom_random );
205
+ STATIC MP_DEFINE_CONST_FUN_OBJ_0 (mod_random_random_obj , mod_random_random );
206
206
207
- STATIC mp_obj_t mod_urandom_uniform (mp_obj_t a_in , mp_obj_t b_in ) {
207
+ STATIC mp_obj_t mod_random_uniform (mp_obj_t a_in , mp_obj_t b_in ) {
208
208
mp_float_t a = mp_obj_get_float (a_in );
209
209
mp_float_t b = mp_obj_get_float (b_in );
210
210
return mp_obj_new_float (a + (b - a ) * yasmarang_float ());
211
211
}
212
- STATIC MP_DEFINE_CONST_FUN_OBJ_2 (mod_urandom_uniform_obj , mod_urandom_uniform );
212
+ STATIC MP_DEFINE_CONST_FUN_OBJ_2 (mod_random_uniform_obj , mod_random_uniform );
213
213
214
214
#endif
215
215
216
- #endif // MICROPY_PY_URANDOM_EXTRA_FUNCS
216
+ #endif // MICROPY_PY_RANDOM_EXTRA_FUNCS
217
217
218
218
#if SEED_ON_IMPORT
219
- STATIC mp_obj_t mod_urandom___init__ (void ) {
219
+ STATIC mp_obj_t mod_random___init__ (void ) {
220
220
// This module may be imported by more than one name so need to ensure
221
221
// that it's only ever seeded once.
222
222
static bool seeded = false;
223
223
if (!seeded ) {
224
224
seeded = true;
225
- mod_urandom_seed (0 , NULL );
225
+ mod_random_seed (0 , NULL );
226
226
}
227
227
return mp_const_none ;
228
228
}
229
- STATIC MP_DEFINE_CONST_FUN_OBJ_0 (mod_urandom___init___obj , mod_urandom___init__ );
229
+ STATIC MP_DEFINE_CONST_FUN_OBJ_0 (mod_random___init___obj , mod_random___init__ );
230
230
#endif
231
231
232
232
#if !MICROPY_ENABLE_DYNRUNTIME
233
- STATIC const mp_rom_map_elem_t mp_module_urandom_globals_table [] = {
234
- { MP_ROM_QSTR (MP_QSTR___name__ ), MP_ROM_QSTR (MP_QSTR_urandom ) },
233
+ STATIC const mp_rom_map_elem_t mp_module_random_globals_table [] = {
234
+ { MP_ROM_QSTR (MP_QSTR___name__ ), MP_ROM_QSTR (MP_QSTR_random ) },
235
235
#if SEED_ON_IMPORT
236
- { MP_ROM_QSTR (MP_QSTR___init__ ), MP_ROM_PTR (& mod_urandom___init___obj ) },
236
+ { MP_ROM_QSTR (MP_QSTR___init__ ), MP_ROM_PTR (& mod_random___init___obj ) },
237
237
#endif
238
- { MP_ROM_QSTR (MP_QSTR_getrandbits ), MP_ROM_PTR (& mod_urandom_getrandbits_obj ) },
239
- { MP_ROM_QSTR (MP_QSTR_seed ), MP_ROM_PTR (& mod_urandom_seed_obj ) },
240
- #if MICROPY_PY_URANDOM_EXTRA_FUNCS
241
- { MP_ROM_QSTR (MP_QSTR_randrange ), MP_ROM_PTR (& mod_urandom_randrange_obj ) },
242
- { MP_ROM_QSTR (MP_QSTR_randint ), MP_ROM_PTR (& mod_urandom_randint_obj ) },
243
- { MP_ROM_QSTR (MP_QSTR_choice ), MP_ROM_PTR (& mod_urandom_choice_obj ) },
238
+ { MP_ROM_QSTR (MP_QSTR_getrandbits ), MP_ROM_PTR (& mod_random_getrandbits_obj ) },
239
+ { MP_ROM_QSTR (MP_QSTR_seed ), MP_ROM_PTR (& mod_random_seed_obj ) },
240
+ #if MICROPY_PY_RANDOM_EXTRA_FUNCS
241
+ { MP_ROM_QSTR (MP_QSTR_randrange ), MP_ROM_PTR (& mod_random_randrange_obj ) },
242
+ { MP_ROM_QSTR (MP_QSTR_randint ), MP_ROM_PTR (& mod_random_randint_obj ) },
243
+ { MP_ROM_QSTR (MP_QSTR_choice ), MP_ROM_PTR (& mod_random_choice_obj ) },
244
244
#if MICROPY_PY_BUILTINS_FLOAT
245
- { MP_ROM_QSTR (MP_QSTR_random ), MP_ROM_PTR (& mod_urandom_random_obj ) },
246
- { MP_ROM_QSTR (MP_QSTR_uniform ), MP_ROM_PTR (& mod_urandom_uniform_obj ) },
245
+ { MP_ROM_QSTR (MP_QSTR_random ), MP_ROM_PTR (& mod_random_random_obj ) },
246
+ { MP_ROM_QSTR (MP_QSTR_uniform ), MP_ROM_PTR (& mod_random_uniform_obj ) },
247
247
#endif
248
248
#endif
249
249
};
250
250
251
- STATIC MP_DEFINE_CONST_DICT (mp_module_urandom_globals , mp_module_urandom_globals_table );
251
+ STATIC MP_DEFINE_CONST_DICT (mp_module_random_globals , mp_module_random_globals_table );
252
252
253
- const mp_obj_module_t mp_module_urandom = {
253
+ const mp_obj_module_t mp_module_random = {
254
254
.base = { & mp_type_module },
255
- .globals = (mp_obj_dict_t * )& mp_module_urandom_globals ,
255
+ .globals = (mp_obj_dict_t * )& mp_module_random_globals ,
256
256
};
257
257
258
- MP_REGISTER_MODULE ( MP_QSTR_urandom , mp_module_urandom );
258
+ MP_REGISTER_EXTENSIBLE_MODULE ( MP_QSTR_random , mp_module_random );
259
259
#endif
260
260
261
- #endif // MICROPY_PY_URANDOM
261
+ #endif // MICROPY_PY_RANDOM
0 commit comments