Skip to content

Commit

Permalink
urandom: Move example to Move Hub.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
laurensvalk committed Dec 1, 2022
1 parent 87ed735 commit 64b0489
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 13 additions & 0 deletions doc/main/hubs/movehub.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 19 additions & 0 deletions examples/pup/hub_movehub/randint_implementation.py
Original file line number Diff line number Diff line change
@@ -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))
12 changes: 0 additions & 12 deletions src/urandom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 64b0489

Please sign in to comment.