Skip to content

Commit 365bad7

Browse files
Set the random number seed. (#87)
It's used in a non-crypto safe RNG in MicroPython so just seed it with a Math.random-derived value. Closes #85
1 parent b88271f commit 365bad7

File tree

5 files changed

+26
-4
lines changed

5 files changed

+26
-4
lines changed

src/demo.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ <h1>MicroPython-micro:bit simulator example embedding</h1>
9393
<option value="music">Music</option>
9494
<option value="pin_logo">Pin logo</option>
9595
<option value="radio">Radio</option>
96+
<option value="random">Random</option>
9697
<option value="sensors">Sensors</option>
9798
<option value="sound_effects_builtin">
9899
Sound effects (builtin)

src/examples/random.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from microbit import display, sleep
2+
import random
3+
import machine
4+
5+
6+
n = random.randrange(0, 100)
7+
print("Default seed ", n)
8+
random.seed(1)
9+
n = random.randrange(0, 100)
10+
print("Fixed seed ", n)
11+
print()
12+
sleep(2000)
13+
machine.reset()

src/jshal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
void mp_js_hal_init(void);
2828
void mp_js_hal_deinit(void);
2929

30+
uint32_t mp_js_rng_generate_random_word();
31+
3032
uint32_t mp_js_hal_ticks_ms(void);
3133
void mp_js_hal_stdout_tx_strn(const char *ptr, size_t len);
3234
int mp_js_hal_stdin_pop_char(void);

src/jshal.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ mergeInto(LibraryManager.library, {
3636
Module.board.stopComponents();
3737
},
3838

39+
mp_js_rng_generate_random_word: function () {
40+
return (Math.random() * 0x100000000) >>> 0;
41+
},
42+
3943
mp_js_hal_ticks_ms: function () {
4044
return Module.board.ticksMilliseconds();
4145
},
@@ -131,7 +135,10 @@ mergeInto(LibraryManager.library, {
131135
return Module.board.pins[pin].getAnalogPeriodUs();
132136
},
133137

134-
mp_js_hal_pin_set_analog_period_us: function (/** @type {number} */ pin, /** @type {number} */ period) {
138+
mp_js_hal_pin_set_analog_period_us: function (
139+
/** @type {number} */ pin,
140+
/** @type {number} */ period
141+
) {
135142
return Module.board.pins[pin].setAnalogPeriodUs(period);
136143
},
137144

src/microbithal_js.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,10 +389,9 @@ int microbit_hal_log_data(const char *key, const char *value) {
389389
return mp_js_hal_log_data(key, value);
390390
}
391391

392-
// This is needed by the microbitfs implementation.
392+
// This is used to seed the random number generator.
393393
uint32_t rng_generate_random_word(void) {
394-
//return uBit.random(65536) << 16 | uBit.random(65536);
395-
return 0;
394+
return mp_js_rng_generate_random_word();
396395
}
397396

398397
void microbit_hal_audio_select_pin(int pin) {

0 commit comments

Comments
 (0)