Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set the random number seed #87

Merged
merged 1 commit into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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