mirror of
https://github.com/pybricks/pybricks-api.git
synced 2026-09-15 02:54:52 +00:00
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.
This commit is contained in:
+4
-1
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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))
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user