From a58a91eb04c50daafb31228a79f6752374338c5e Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 17 Jan 2016 12:10:28 +0200 Subject: extmod/modurandom: Add "urandom" module. Seedable and reproducible pseudo-random number generator. Implemented functions are getrandbits(n) (n <= 32) and seed(). The algorithm used is Yasmarang by Ilya Levin: http://www.literatecode.com/yasmarang --- py/builtin.h | 1 + py/mpconfig.h | 4 ++++ py/objmodule.c | 3 +++ py/py.mk | 1 + py/qstrdefs.h | 6 ++++++ 5 files changed, 15 insertions(+) (limited to 'py') diff --git a/py/builtin.h b/py/builtin.h index 261e5a231..162835cd4 100644 --- a/py/builtin.h +++ b/py/builtin.h @@ -101,6 +101,7 @@ extern const mp_obj_module_t mp_module_ure; extern const mp_obj_module_t mp_module_uheapq; extern const mp_obj_module_t mp_module_uhashlib; extern const mp_obj_module_t mp_module_ubinascii; +extern const mp_obj_module_t mp_module_urandom; extern const mp_obj_module_t mp_module_ussl; extern const mp_obj_module_t mp_module_machine; extern const mp_obj_module_t mp_module_lwip; diff --git a/py/mpconfig.h b/py/mpconfig.h index 5e717ad0f..72af343dd 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -783,6 +783,10 @@ typedef double mp_float_t; #define MICROPY_PY_UBINASCII (0) #endif +#ifndef MICROPY_PY_URANDOM +#define MICROPY_PY_URANDOM (0) +#endif + #ifndef MICROPY_PY_MACHINE #define MICROPY_PY_MACHINE (0) #endif diff --git a/py/objmodule.c b/py/objmodule.c index 32ded8919..1034d00f6 100644 --- a/py/objmodule.c +++ b/py/objmodule.c @@ -184,6 +184,9 @@ STATIC const mp_rom_map_elem_t mp_builtin_module_table[] = { #if MICROPY_PY_UBINASCII { MP_ROM_QSTR(MP_QSTR_ubinascii), MP_ROM_PTR(&mp_module_ubinascii) }, #endif +#if MICROPY_PY_URANDOM + { MP_ROM_QSTR(MP_QSTR_urandom), MP_ROM_PTR(&mp_module_urandom) }, +#endif #if MICROPY_PY_USSL { MP_ROM_QSTR(MP_QSTR_ussl), MP_ROM_PTR(&mp_module_ussl) }, #endif diff --git a/py/py.mk b/py/py.mk index 4574ec9a6..63770e2d2 100644 --- a/py/py.mk +++ b/py/py.mk @@ -168,6 +168,7 @@ PY_O_BASENAME = \ ../extmod/modubinascii.o \ ../extmod/machine_mem.o \ ../extmod/modussl.o \ + ../extmod/modurandom.o \ ../extmod/fsusermount.o \ ../extmod/moduos_dupterm.o \ diff --git a/py/qstrdefs.h b/py/qstrdefs.h index 50f2b3b38..7e2109006 100644 --- a/py/qstrdefs.h +++ b/py/qstrdefs.h @@ -671,3 +671,9 @@ Q(count) #if MICROPY_PY_OS_DUPTERM Q(dupterm) #endif + +#if MICROPY_PY_URANDOM +Q(urandom) +Q(getrandbits) +Q(seed) +#endif -- cgit v1.2.3