2016-01-17 10:10:28 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016 Paul Sokolovsky
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "py/runtime.h"
|
|
|
|
|
2022-08-18 06:01:26 +01:00
|
|
|
#if MICROPY_PY_RANDOM
|
2016-01-17 10:10:28 +00:00
|
|
|
|
2020-10-20 04:20:00 +01:00
|
|
|
// Work out if the seed will be set on import or not.
|
2022-08-18 06:01:26 +01:00
|
|
|
#if MICROPY_MODULE_BUILTIN_INIT && defined(MICROPY_PY_RANDOM_SEED_INIT_FUNC)
|
2020-10-20 04:20:00 +01:00
|
|
|
#define SEED_ON_IMPORT (1)
|
|
|
|
#else
|
|
|
|
#define SEED_ON_IMPORT (0)
|
|
|
|
#endif
|
|
|
|
|
2016-01-17 10:10:28 +00:00
|
|
|
// Yasmarang random number generator
|
|
|
|
// by Ilya Levin
|
|
|
|
// http://www.literatecode.com/yasmarang
|
|
|
|
// Public Domain
|
|
|
|
|
2019-12-13 02:30:44 +00:00
|
|
|
#if !MICROPY_ENABLE_DYNRUNTIME
|
2020-10-20 04:20:00 +01:00
|
|
|
#if SEED_ON_IMPORT
|
|
|
|
// If the state is seeded on import then keep these variables in the BSS.
|
|
|
|
STATIC uint32_t yasmarang_pad, yasmarang_n, yasmarang_d;
|
|
|
|
STATIC uint8_t yasmarang_dat;
|
|
|
|
#else
|
|
|
|
// Without seed-on-import these variables must be initialised via the data section.
|
2016-01-17 10:10:28 +00:00
|
|
|
STATIC uint32_t yasmarang_pad = 0xeda4baba, yasmarang_n = 69, yasmarang_d = 233;
|
|
|
|
STATIC uint8_t yasmarang_dat = 0;
|
2019-12-13 02:30:44 +00:00
|
|
|
#endif
|
2020-10-20 04:20:00 +01:00
|
|
|
#endif
|
2016-01-17 10:10:28 +00:00
|
|
|
|
2016-01-17 11:13:57 +00:00
|
|
|
STATIC uint32_t yasmarang(void) {
|
2016-01-17 10:10:28 +00:00
|
|
|
yasmarang_pad += yasmarang_dat + yasmarang_d * yasmarang_n;
|
|
|
|
yasmarang_pad = (yasmarang_pad << 3) + (yasmarang_pad >> 29);
|
|
|
|
yasmarang_n = yasmarang_pad | 2;
|
|
|
|
yasmarang_d ^= (yasmarang_pad << 31) + (yasmarang_pad >> 1);
|
|
|
|
yasmarang_dat ^= (char)yasmarang_pad ^ (yasmarang_d >> 8) ^ 1;
|
2020-02-27 04:36:53 +00:00
|
|
|
|
2016-01-17 10:10:28 +00:00
|
|
|
return yasmarang_pad ^ (yasmarang_d << 5) ^ (yasmarang_pad >> 18) ^ (yasmarang_dat << 1);
|
|
|
|
} /* yasmarang */
|
|
|
|
|
|
|
|
// End of Yasmarang
|
|
|
|
|
2022-08-18 06:01:26 +01:00
|
|
|
#if MICROPY_PY_RANDOM_EXTRA_FUNCS
|
2016-01-22 16:19:32 +00:00
|
|
|
|
|
|
|
// returns an unsigned integer below the given argument
|
|
|
|
// n must not be zero
|
|
|
|
STATIC uint32_t yasmarang_randbelow(uint32_t n) {
|
|
|
|
uint32_t mask = 1;
|
|
|
|
while ((n & mask) < n) {
|
|
|
|
mask = (mask << 1) | 1;
|
|
|
|
}
|
|
|
|
uint32_t r;
|
|
|
|
do {
|
|
|
|
r = yasmarang() & mask;
|
|
|
|
} while (r >= n);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC mp_obj_t mod_random_getrandbits(mp_obj_t num_in) {
|
2016-01-17 10:10:28 +00:00
|
|
|
int n = mp_obj_get_int(num_in);
|
2021-05-30 07:52:08 +01:00
|
|
|
if (n > 32 || n < 0) {
|
2020-12-23 17:05:26 +00:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("bits must be 32 or less"));
|
2016-01-17 10:10:28 +00:00
|
|
|
}
|
2021-05-30 07:52:08 +01:00
|
|
|
if (n == 0) {
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(0);
|
|
|
|
}
|
2016-01-17 10:10:28 +00:00
|
|
|
uint32_t mask = ~0;
|
|
|
|
// Beware of C undefined behavior when shifting by >= than bit size
|
|
|
|
mask >>= (32 - n);
|
|
|
|
return mp_obj_new_int_from_uint(yasmarang() & mask);
|
|
|
|
}
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_random_getrandbits_obj, mod_random_getrandbits);
|
2016-01-17 10:10:28 +00:00
|
|
|
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC mp_obj_t mod_random_seed(size_t n_args, const mp_obj_t *args) {
|
2020-10-20 04:20:00 +01:00
|
|
|
mp_uint_t seed;
|
|
|
|
if (n_args == 0 || args[0] == mp_const_none) {
|
2022-08-18 06:01:26 +01:00
|
|
|
#ifdef MICROPY_PY_RANDOM_SEED_INIT_FUNC
|
|
|
|
seed = MICROPY_PY_RANDOM_SEED_INIT_FUNC;
|
2020-10-20 04:20:00 +01:00
|
|
|
#else
|
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("no default seed"));
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
seed = mp_obj_get_int_truncated(args[0]);
|
|
|
|
}
|
2016-01-17 10:10:28 +00:00
|
|
|
yasmarang_pad = seed;
|
|
|
|
yasmarang_n = 69;
|
|
|
|
yasmarang_d = 233;
|
|
|
|
yasmarang_dat = 0;
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_random_seed_obj, 0, 1, mod_random_seed);
|
2016-01-17 10:10:28 +00:00
|
|
|
|
2022-08-18 06:01:26 +01:00
|
|
|
#if MICROPY_PY_RANDOM_EXTRA_FUNCS
|
2016-01-22 16:19:32 +00:00
|
|
|
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC mp_obj_t mod_random_randrange(size_t n_args, const mp_obj_t *args) {
|
2016-01-22 16:19:32 +00:00
|
|
|
mp_int_t start = mp_obj_get_int(args[0]);
|
|
|
|
if (n_args == 1) {
|
|
|
|
// range(stop)
|
|
|
|
if (start > 0) {
|
|
|
|
return mp_obj_new_int(yasmarang_randbelow(start));
|
|
|
|
} else {
|
2017-02-24 15:04:23 +00:00
|
|
|
goto error;
|
2016-01-22 16:19:32 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mp_int_t stop = mp_obj_get_int(args[1]);
|
|
|
|
if (n_args == 2) {
|
|
|
|
// range(start, stop)
|
|
|
|
if (start < stop) {
|
|
|
|
return mp_obj_new_int(start + yasmarang_randbelow(stop - start));
|
|
|
|
} else {
|
2017-02-24 15:04:23 +00:00
|
|
|
goto error;
|
2016-01-22 16:19:32 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// range(start, stop, step)
|
|
|
|
mp_int_t step = mp_obj_get_int(args[2]);
|
|
|
|
mp_int_t n;
|
|
|
|
if (step > 0) {
|
|
|
|
n = (stop - start + step - 1) / step;
|
|
|
|
} else if (step < 0) {
|
|
|
|
n = (stop - start + step + 1) / step;
|
|
|
|
} else {
|
2017-02-24 15:04:23 +00:00
|
|
|
goto error;
|
2016-01-22 16:19:32 +00:00
|
|
|
}
|
|
|
|
if (n > 0) {
|
|
|
|
return mp_obj_new_int(start + step * yasmarang_randbelow(n));
|
|
|
|
} else {
|
2017-02-24 15:04:23 +00:00
|
|
|
goto error;
|
2016-01-22 16:19:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-24 15:04:23 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
mp_raise_ValueError(NULL);
|
2016-01-22 16:19:32 +00:00
|
|
|
}
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_random_randrange_obj, 1, 3, mod_random_randrange);
|
2016-01-22 16:19:32 +00:00
|
|
|
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC mp_obj_t mod_random_randint(mp_obj_t a_in, mp_obj_t b_in) {
|
2016-01-22 16:19:32 +00:00
|
|
|
mp_int_t a = mp_obj_get_int(a_in);
|
|
|
|
mp_int_t b = mp_obj_get_int(b_in);
|
|
|
|
if (a <= b) {
|
|
|
|
return mp_obj_new_int(a + yasmarang_randbelow(b - a + 1));
|
|
|
|
} else {
|
2017-02-24 15:04:23 +00:00
|
|
|
mp_raise_ValueError(NULL);
|
2016-01-22 16:19:32 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_random_randint_obj, mod_random_randint);
|
2016-01-22 16:19:32 +00:00
|
|
|
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC mp_obj_t mod_random_choice(mp_obj_t seq) {
|
2016-01-22 16:19:32 +00:00
|
|
|
mp_int_t len = mp_obj_get_int(mp_obj_len(seq));
|
|
|
|
if (len > 0) {
|
|
|
|
return mp_obj_subscr(seq, mp_obj_new_int(yasmarang_randbelow(len)), MP_OBJ_SENTINEL);
|
|
|
|
} else {
|
2020-02-11 02:17:41 +00:00
|
|
|
mp_raise_type(&mp_type_IndexError);
|
2016-01-22 16:19:32 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_random_choice_obj, mod_random_choice);
|
2016-01-22 16:19:32 +00:00
|
|
|
|
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
|
|
|
|
|
|
|
// returns a number in the range [0..1) using Yasmarang to fill in the fraction bits
|
|
|
|
STATIC mp_float_t yasmarang_float(void) {
|
2020-02-14 01:26:46 +00:00
|
|
|
mp_float_union_t u;
|
2016-01-22 16:19:32 +00:00
|
|
|
u.p.sgn = 0;
|
|
|
|
u.p.exp = (1 << (MP_FLOAT_EXP_BITS - 1)) - 1;
|
|
|
|
if (MP_FLOAT_FRAC_BITS <= 32) {
|
|
|
|
u.p.frc = yasmarang();
|
|
|
|
} else {
|
|
|
|
u.p.frc = ((uint64_t)yasmarang() << 32) | (uint64_t)yasmarang();
|
|
|
|
}
|
|
|
|
return u.f - 1;
|
|
|
|
}
|
|
|
|
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC mp_obj_t mod_random_random(void) {
|
2016-01-22 16:19:32 +00:00
|
|
|
return mp_obj_new_float(yasmarang_float());
|
|
|
|
}
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_random_random_obj, mod_random_random);
|
2016-01-22 16:19:32 +00:00
|
|
|
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC mp_obj_t mod_random_uniform(mp_obj_t a_in, mp_obj_t b_in) {
|
2016-01-22 16:19:32 +00:00
|
|
|
mp_float_t a = mp_obj_get_float(a_in);
|
|
|
|
mp_float_t b = mp_obj_get_float(b_in);
|
|
|
|
return mp_obj_new_float(a + (b - a) * yasmarang_float());
|
|
|
|
}
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_random_uniform_obj, mod_random_uniform);
|
2016-01-22 16:19:32 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2022-08-18 06:01:26 +01:00
|
|
|
#endif // MICROPY_PY_RANDOM_EXTRA_FUNCS
|
2016-01-22 16:19:32 +00:00
|
|
|
|
2020-10-20 04:20:00 +01:00
|
|
|
#if SEED_ON_IMPORT
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC mp_obj_t mod_random___init__(void) {
|
2020-10-20 04:20:00 +01:00
|
|
|
// This module may be imported by more than one name so need to ensure
|
|
|
|
// that it's only ever seeded once.
|
|
|
|
static bool seeded = false;
|
|
|
|
if (!seeded) {
|
|
|
|
seeded = true;
|
2022-08-18 05:49:57 +01:00
|
|
|
mod_random_seed(0, NULL);
|
2020-10-20 04:20:00 +01:00
|
|
|
}
|
2019-04-11 10:03:05 +01:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2022-08-18 05:49:57 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_random___init___obj, mod_random___init__);
|
2019-04-11 10:03:05 +01:00
|
|
|
#endif
|
|
|
|
|
2019-12-13 02:30:44 +00:00
|
|
|
#if !MICROPY_ENABLE_DYNRUNTIME
|
2022-08-18 05:47:56 +01:00
|
|
|
STATIC const mp_rom_map_elem_t mp_module_random_globals_table[] = {
|
2022-08-17 07:18:31 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_random) },
|
2020-10-20 04:20:00 +01:00
|
|
|
#if SEED_ON_IMPORT
|
2022-08-18 05:49:57 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR___init__), MP_ROM_PTR(&mod_random___init___obj) },
|
2019-04-11 10:03:05 +01:00
|
|
|
#endif
|
2022-08-18 05:49:57 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_getrandbits), MP_ROM_PTR(&mod_random_getrandbits_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_seed), MP_ROM_PTR(&mod_random_seed_obj) },
|
2022-08-18 06:01:26 +01:00
|
|
|
#if MICROPY_PY_RANDOM_EXTRA_FUNCS
|
2022-08-18 05:49:57 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_randrange), MP_ROM_PTR(&mod_random_randrange_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_randint), MP_ROM_PTR(&mod_random_randint_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_choice), MP_ROM_PTR(&mod_random_choice_obj) },
|
2016-11-30 01:31:44 +00:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2022-08-18 05:49:57 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_random), MP_ROM_PTR(&mod_random_random_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_uniform), MP_ROM_PTR(&mod_random_uniform_obj) },
|
2016-01-22 16:19:32 +00:00
|
|
|
#endif
|
2016-11-30 01:31:44 +00:00
|
|
|
#endif
|
2016-01-17 10:10:28 +00:00
|
|
|
};
|
|
|
|
|
2022-08-18 05:47:56 +01:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(mp_module_random_globals, mp_module_random_globals_table);
|
2016-01-17 10:10:28 +00:00
|
|
|
|
2022-08-18 05:47:56 +01:00
|
|
|
const mp_obj_module_t mp_module_random = {
|
2016-01-17 10:10:28 +00:00
|
|
|
.base = { &mp_type_module },
|
2022-08-18 05:47:56 +01:00
|
|
|
.globals = (mp_obj_dict_t *)&mp_module_random_globals,
|
2016-01-17 10:10:28 +00:00
|
|
|
};
|
2022-04-20 07:06:22 +01:00
|
|
|
|
2023-06-02 03:33:25 +01:00
|
|
|
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_random, mp_module_random);
|
2019-12-13 02:30:44 +00:00
|
|
|
#endif
|
2016-01-17 10:10:28 +00:00
|
|
|
|
2022-08-18 06:01:26 +01:00
|
|
|
#endif // MICROPY_PY_RANDOM
|