From 64b0489a73b155d705e141b2dce4da0787c440fd Mon Sep 17 00:00:00 2001 From: Laurens Valk Date: Thu, 1 Dec 2022 09:55:40 +0100 Subject: [PATCH] urandom: Move example to Move Hub. Move it do a dedicated example instead of hardcoding it in the module docstring. Also update changelog for missing previous changes while we are at it. --- CHANGELOG.md | 5 ++++- doc/main/hubs/movehub.rst | 13 +++++++++++++ .../pup/hub_movehub/randint_implementation.py | 19 +++++++++++++++++++ src/urandom/__init__.py | 12 ------------ 4 files changed, 36 insertions(+), 13 deletions(-) create mode 100644 examples/pup/hub_movehub/randint_implementation.py diff --git a/CHANGELOG.md b/CHANGELOG.md index b27bb0d1..f78e4805 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,11 +6,14 @@ ### Added - Documented ``Stop.NONE`` and ``Stop.COAST_SMART``. +- Documented ``ujson`` module. ### Changed - Changed `PrimeHub.display.image()` to `PrimeHub.display.icon()` and renamed its kwarg from `image` to `icon`. -- Improved presentation and docstrings of the ``ubuiltins`` module. +- Improved presentation and docstrings of the ``ubuiltins`` and other + MicroPython modules +- Moved the random numbers example for Move Hub to the Move Hub page. ## 3.2.0b5 - 2022-11-11 diff --git a/doc/main/hubs/movehub.rst b/doc/main/hubs/movehub.rst index 41082e18..393d2d3f 100644 --- a/doc/main/hubs/movehub.rst +++ b/doc/main/hubs/movehub.rst @@ -95,3 +95,16 @@ Turning the hub off .. literalinclude:: ../../../examples/pup/hub_common/build/system_shutdown_movehub.py + +Making random numbers +************************************************** + +The Move Hub does not include the :mod:`urandom` module. If you need random +numbers in your application, you can try a variation of the following example. + +To make it work better, change the initial value of ``_rand`` to something +that is truly random in your application. You could use the IMU acceleration +or a sensor value, for example. + +.. literalinclude:: + ../../../examples/pup/hub_movehub/randint_implementation.py diff --git a/examples/pup/hub_movehub/randint_implementation.py b/examples/pup/hub_movehub/randint_implementation.py new file mode 100644 index 00000000..28a12e39 --- /dev/null +++ b/examples/pup/hub_movehub/randint_implementation.py @@ -0,0 +1,19 @@ +from pybricks.hubs import MoveHub + +# Initialize the hub. +hub = MoveHub() + +# Initialize "random" seed. +_rand = hub.battery.voltage() + hub.battery.current() + + +# Return a random integer N such that a <= N <= b. +def randint(a, b): + global _rand + _rand = 75 * _rand % 65537 # Lehmer + return _rand * (b - a + 1) // 65537 + a + + +# Generate a few example numbers. +for i in range(5): + print(randint(0, 1000)) diff --git a/src/urandom/__init__.py b/src/urandom/__init__.py index 5ee8a06a..6e8a902b 100644 --- a/src/urandom/__init__.py +++ b/src/urandom/__init__.py @@ -8,18 +8,6 @@ """ This module implements pseudo-random number generators. - -.. note:: This module is not available on the BOOST Move Hub. - - You can make your own random number generator like this instead:: - - _rand = hub.battery.voltage() + hub.battery.current() # seed - - # Return a random integer N such that a <= N <= b. - def randint(a, b): - global _rand - _rand = 75 * _rand % 65537 # Lehmer - return _rand * (b - a + 1) // 65537 + a """ from typing import Any, Optional, Sequence, overload