|
| 1 | +/* |
| 2 | + * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2016 Paul Sokolovsky |
| 7 | + * Copyright (c) 2016 Scott Shawcroft for Adafruit Industries |
| 8 | + * |
| 9 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | + * of this software and associated documentation files (the "Software"), to deal |
| 11 | + * in the Software without restriction, including without limitation the rights |
| 12 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | + * copies of the Software, and to permit persons to whom the Software is |
| 14 | + * furnished to do so, subject to the following conditions: |
| 15 | + * |
| 16 | + * The above copyright notice and this permission notice shall be included in |
| 17 | + * all copies or substantial portions of the Software. |
| 18 | + * |
| 19 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | + * THE SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | +#include <assert.h> |
| 29 | +#include <string.h> |
| 30 | + |
| 31 | +#include "py/obj.h" |
| 32 | +#include "py/runtime.h" |
| 33 | +#include "shared-bindings/random/__init__.h" |
| 34 | + |
| 35 | +//| :mod:`random` --- psuedo-random numbers and choices |
| 36 | +//| ======================================================== |
| 37 | +//| |
| 38 | +//| .. module:: random |
| 39 | +//| :synopsis: psuedo-random numbers and choices |
| 40 | +//| :platform: SAMD21, ESP8266 |
| 41 | +//| |
| 42 | +//| The `random` module is a strict subset of the CPython `cpython:random` |
| 43 | +//| module. So, code written in CircuitPython will work in CPython but not |
| 44 | +//| necessarily the other way around. |
| 45 | +//| |
| 46 | +//| Like its CPython cousin, CircuitPython's random seeds itself on first use |
| 47 | +//| with a true random from os.urandom() when available or the uptime otherwise. |
| 48 | +//| Once seeded, it will be deterministic, which is why its bad for cryptography. |
| 49 | +//| |
| 50 | +//| .. warning:: Numbers from this module are not cryptographically strong! Use |
| 51 | +//| bytes from `os.urandom` directly for true randomness. |
| 52 | +//| |
| 53 | + |
| 54 | +//| .. function:: seed(seed) |
| 55 | +//| |
| 56 | +//| Sets the starting seed of the random number generation. Further calls to |
| 57 | +//| `random` will return deterministic results afterwards. |
| 58 | +//| |
| 59 | +STATIC mp_obj_t random_seed(mp_obj_t seed_in) { |
| 60 | + mp_uint_t seed = mp_obj_get_int_truncated(seed_in); |
| 61 | + shared_modules_random_seed(seed); |
| 62 | + return mp_const_none; |
| 63 | +} |
| 64 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(random_seed_obj, random_seed); |
| 65 | + |
| 66 | +//| .. function:: getrandbits(k) |
| 67 | +//| |
| 68 | +//| Returns an integer with *k* random bits. |
| 69 | +//| |
| 70 | +STATIC mp_obj_t random_getrandbits(mp_obj_t num_in) { |
| 71 | + int n = mp_obj_get_int(num_in); |
| 72 | + if (n > 32 || n == 0) { |
| 73 | + mp_raise_ValueError(NULL); |
| 74 | + } |
| 75 | + return mp_obj_new_int_from_uint(shared_modules_random_getrandbits((uint8_t) n)); |
| 76 | +} |
| 77 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(random_getrandbits_obj, random_getrandbits); |
| 78 | + |
| 79 | +//| .. function:: randrange(stop) |
| 80 | +//| randrange(start, stop, step=1) |
| 81 | +//| |
| 82 | +//| Returns a randomly selected integer from `range(start, stop, step)`. |
| 83 | +//| |
| 84 | +STATIC mp_obj_t random_randrange(size_t n_args, const mp_obj_t *args) { |
| 85 | + mp_int_t start = 0; |
| 86 | + mp_int_t stop = mp_obj_get_int(args[0]); |
| 87 | + mp_int_t step = 1; |
| 88 | + if (n_args == 1) { |
| 89 | + // range(stop) |
| 90 | + if (stop <= 0) { |
| 91 | + mp_raise_ValueError("stop not reachable from start"); |
| 92 | + } |
| 93 | + } else { |
| 94 | + start = stop; |
| 95 | + stop = mp_obj_get_int(args[1]); |
| 96 | + if (n_args == 2) { |
| 97 | + // range(start, stop) |
| 98 | + if (start >= stop) { |
| 99 | + mp_raise_ValueError("stop not reachable from start"); |
| 100 | + } |
| 101 | + } else { |
| 102 | + // range(start, stop, step) |
| 103 | + step = mp_obj_get_int(args[2]); |
| 104 | + mp_int_t n; |
| 105 | + if (step > 0) { |
| 106 | + n = (stop - start + step - 1) / step; |
| 107 | + } else if (step < 0) { |
| 108 | + n = (stop - start + step + 1) / step; |
| 109 | + } else { |
| 110 | + mp_raise_ValueError("step must be non-zero"); |
| 111 | + } |
| 112 | + if (n <= 0) { |
| 113 | + mp_raise_ValueError("invalid step"); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return mp_obj_new_int(shared_modules_random_randrange(start, stop, step)); |
| 119 | +} |
| 120 | +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(random_randrange_obj, 1, 3, random_randrange); |
| 121 | + |
| 122 | +//| .. function:: randint(a, b) |
| 123 | +//| |
| 124 | +//| Returns a randomly selected integer between a and b inclusive. Equivalent |
| 125 | +//| to `randrange(a, b + 1, 1)` |
| 126 | +//| |
| 127 | +STATIC mp_obj_t random_randint(mp_obj_t a_in, mp_obj_t b_in) { |
| 128 | + mp_int_t a = mp_obj_get_int(a_in); |
| 129 | + mp_int_t b = mp_obj_get_int(b_in); |
| 130 | + if (a > b) { |
| 131 | + mp_raise_ValueError(NULL); |
| 132 | + } |
| 133 | + return mp_obj_new_int(shared_modules_random_randrange(a, b + 1, 1)); |
| 134 | +} |
| 135 | +STATIC MP_DEFINE_CONST_FUN_OBJ_2(random_randint_obj, random_randint); |
| 136 | + |
| 137 | +//| .. function:: choice(seq) |
| 138 | +//| |
| 139 | +//| Returns a randomly selected element from the given sequence. Raises |
| 140 | +//| IndexError when the sequence is empty. |
| 141 | +//| |
| 142 | +STATIC mp_obj_t random_choice(mp_obj_t seq) { |
| 143 | + mp_int_t len = mp_obj_get_int(mp_obj_len(seq)); |
| 144 | + if (len == 0) { |
| 145 | + mp_raise_IndexError("empty sequence"); |
| 146 | + } |
| 147 | + return mp_obj_subscr(seq, mp_obj_new_int(shared_modules_random_randrange(0, len, 1)), MP_OBJ_SENTINEL); |
| 148 | +} |
| 149 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(random_choice_obj, random_choice); |
| 150 | + |
| 151 | +//| .. function:: random() |
| 152 | +//| |
| 153 | +//| Returns a random float between 0 and 1.0. |
| 154 | +//| |
| 155 | +STATIC mp_obj_t random_random(void) { |
| 156 | + return mp_obj_new_float(shared_modules_random_random()); |
| 157 | +} |
| 158 | +STATIC MP_DEFINE_CONST_FUN_OBJ_0(random_random_obj, random_random); |
| 159 | + |
| 160 | +//| .. function:: uniform(a, b) |
| 161 | +//| |
| 162 | +//| Returns a random float between a and b. It may or may not be inclusive |
| 163 | +//| depending on float rounding. |
| 164 | +//| |
| 165 | +STATIC mp_obj_t random_uniform(mp_obj_t a_in, mp_obj_t b_in) { |
| 166 | + mp_float_t a = mp_obj_get_float(a_in); |
| 167 | + mp_float_t b = mp_obj_get_float(b_in); |
| 168 | + return mp_obj_new_float(shared_modules_random_uniform(a, b)); |
| 169 | +} |
| 170 | +STATIC MP_DEFINE_CONST_FUN_OBJ_2(random_uniform_obj, random_uniform); |
| 171 | + |
| 172 | +STATIC const mp_rom_map_elem_t mp_module_random_globals_table[] = { |
| 173 | + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_random) }, |
| 174 | + { MP_ROM_QSTR(MP_QSTR_seed), MP_ROM_PTR(&random_seed_obj) }, |
| 175 | + { MP_ROM_QSTR(MP_QSTR_getrandbits), MP_ROM_PTR(&random_getrandbits_obj) }, |
| 176 | + { MP_ROM_QSTR(MP_QSTR_randrange), MP_ROM_PTR(&random_randrange_obj) }, |
| 177 | + { MP_ROM_QSTR(MP_QSTR_randint), MP_ROM_PTR(&random_randint_obj) }, |
| 178 | + { MP_ROM_QSTR(MP_QSTR_choice), MP_ROM_PTR(&random_choice_obj) }, |
| 179 | + { MP_ROM_QSTR(MP_QSTR_random), MP_ROM_PTR(&random_random_obj) }, |
| 180 | + { MP_ROM_QSTR(MP_QSTR_uniform), MP_ROM_PTR(&random_uniform_obj) }, |
| 181 | +}; |
| 182 | + |
| 183 | +STATIC MP_DEFINE_CONST_DICT(mp_module_random_globals, mp_module_random_globals_table); |
| 184 | + |
| 185 | +const mp_obj_module_t random_module = { |
| 186 | + .base = { &mp_type_module }, |
| 187 | + .globals = (mp_obj_dict_t*)&mp_module_random_globals, |
| 188 | +}; |
0 commit comments