Skip to content

Commit

Permalink
Set the random number seed.
Browse files Browse the repository at this point in the history
It's used in a non-crypto safe RNG in MicroPython so just seed it with a
Math.random-derived value.

Closes #85
  • Loading branch information
microbit-matt-hillsdon committed Oct 31, 2022
1 parent b88271f commit 09aebd0
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ <h1>MicroPython-micro:bit simulator example embedding</h1>
<option value="music">Music</option>
<option value="pin_logo">Pin logo</option>
<option value="radio">Radio</option>
<option value="random">Random</option>
<option value="sensors">Sensors</option>
<option value="sound_effects_builtin">
Sound effects (builtin)
Expand Down
13 changes: 13 additions & 0 deletions src/examples/random.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from microbit import display, sleep
import random
import machine


n = random.randrange(0, 100)
print("Default seed ", n)
random.seed(1)
n = random.randrange(0, 100)
print("Fixed seed ", n)
print()
sleep(2000)
machine.reset()
2 changes: 2 additions & 0 deletions src/jshal.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
void mp_js_hal_init(void);
void mp_js_hal_deinit(void);

uint32_t mp_js_rng_generate_random_word();

uint32_t mp_js_hal_ticks_ms(void);
void mp_js_hal_stdout_tx_strn(const char *ptr, size_t len);
int mp_js_hal_stdin_pop_char(void);
Expand Down
9 changes: 8 additions & 1 deletion src/jshal.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ mergeInto(LibraryManager.library, {
Module.board.stopComponents();
},

mp_js_rng_generate_random_word: function () {
return (Math.random() * 0x100000000) >>> 0;
},

mp_js_hal_ticks_ms: function () {
return Module.board.ticksMilliseconds();
},
Expand Down Expand Up @@ -131,7 +135,10 @@ mergeInto(LibraryManager.library, {
return Module.board.pins[pin].getAnalogPeriodUs();
},

mp_js_hal_pin_set_analog_period_us: function (/** @type {number} */ pin, /** @type {number} */ period) {
mp_js_hal_pin_set_analog_period_us: function (
/** @type {number} */ pin,
/** @type {number} */ period
) {
return Module.board.pins[pin].setAnalogPeriodUs(period);
},

Expand Down
5 changes: 2 additions & 3 deletions src/microbithal_js.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,9 @@ int microbit_hal_log_data(const char *key, const char *value) {
return mp_js_hal_log_data(key, value);
}

// This is needed by the microbitfs implementation.
// This is used to seed the random number generator.
uint32_t rng_generate_random_word(void) {
//return uBit.random(65536) << 16 | uBit.random(65536);
return 0;
return mp_js_rng_generate_random_word();
}

void microbit_hal_audio_select_pin(int pin) {
Expand Down

0 comments on commit 09aebd0

Please sign in to comment.