-
Notifications
You must be signed in to change notification settings - Fork 590
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
randint() not random when bluetooth enabled #5409
Comments
I can reproduce the bug on v2 hardware. The sequence of numbers are always 1,7,8 etc when the hardware is reset and random number is generated on button A pressed. |
@Amerlander would you be able to provide a C++ example that replicates the issue in CODAL? that will help us replicating it and debugging it. |
@microbit-carlos unfortunately no, that's why I think this is a microbit-pxt and not a DAL/CODAL issue, as mentioned in my initial post:
It produces non-random ints, when The code, that should - but actually doesn't - replicate this issue in CODAL would be: #include "MicroBit.h"
MicroBit uBit;
int main()
{
uBit.init();
int randomInt = uBit.random(9);
uBit.display.print(randomInt);
} {
"target": {
"name": "codal-microbit-v2",
"url": "https://github.com/lancaster-university/codal-microbit-v2",
"branch": "master",
"type": "git"
},
"config":{
"MICROBIT_BLE_ENABLED" : 1
}
} |
Thanks @Amerlander! Would adding this code to the CODAL example make a difference? pxt-microbit/libs/core/codal.cpp Lines 52 to 57 in 53e49a9
seedRandom() is probably from here, but assuming the seed is different every time it should technically work: https://github.com/microsoft/pxt-common-packages/blob/5148fecf59dbbef876ac53c3a877d814da6de0dd/libs/base/core.cpp#L333 |
@abchatra I couldn't easily find the |
Yes. Using this code reproduces the issue, when BLE is enabled and is working when BLE is disabled main.cpp in microbit-v2-samples:
codal.json that produces random ints:
Changing the Config to enable BLE will always give the same number as first random int:
|
Uncommenting the line pxt-microbit/libs/core/codal.cpp Line 53 in 53e49a9
It seems like if But I'm not that much into the CODAL functions, so I might be wrong. |
@microbit-carlos I am going to assign this bug to you and move to hotfix milestone as the fix seems to be on the codal side. |
It is not needed, as the random number generator is already seeded in uBit.init(). This also works around this issue until it is resolved in DAL & CODAL: #5409 Apart from that it is a costly call, specially in V1 where it takes 50 microsec or more, and in theory can take up to 480 us.
We've been looking into this and @Amerlander is spot-on (thank you btw for the bug report, the debugging and analysis!).
pxt-microbit/libs/core/codal.cpp Line 53 in 7cc19bb
That being said, calling We'll do a CODAL tag with the fix soon, but realistically it will be a while before we release the next DAL tag. So, I've also created this PR in MakeCode which removes the unnecessary call to |
It is not needed, as the random number generator is already seeded in uBit.init(). This also works around this issue until it is resolved in DAL & CODAL: #5409 Apart from that it is a costly call, specially in V1 where it takes 50 microsec or more, and in theory can take up to 480 us.
It is not needed, as the random number generator is already seeded in uBit.init(). This also works around this issue until it is resolved in DAL & CODAL: #5409 Apart from that it is a costly call, specially in V1 where it takes 50 microsec or more, and in theory can take up to 480 us.
@abchatra I can confirm that with #5945 this issue has been resolved in https://makecode.microbit.org/stable |
Describe the bug
When pxt is set to have bluetooth anabled the randint() block always produces the same number. Regardless of v1 or v2.
To Reproduce
Steps to reproduce the behavior:
basic.showNumber(randint(0, 9))
Alternative check this shared project where ble was enabled without loading the bluetooth extension: https://makecode.microbit.org/S91179-28567-56186-10264
Expected behavior
I expect the microbit to show a random number that is not predictable.
micro:bit version (please complete the following information):
Tested on 1.3, but happens on v1 and v2 afaik.
Additional context
On v1 the seedrandom function checks if ble is enabled. If so it tries to use nrf functions for generating a seed and if that fails it is a fix seed. On v2 it is always a fix seed if ble is enabled: https://github.com/lancaster-university/microbit-dal/blob/602153e9199c28c08fd2561ce7b43bf64e9e7394/source/core/MicroBitDevice.cpp#L340
The init function cals
seedrandom()
: https://github.com/lancaster-university/microbit/blob/1d1baa561f82ab0d16d1916b0026955c67d95e74/source/MicroBit.cpp#L118C18-L118C18But since it gets called before ble is started, it should not matter what value in
BLE_ENABLED
is set.randint()
is defined in pxt, but I didn't figure out how this actually calls the microbit random number functions. Since I was not able to reproduce this issue in the samples, I wonder if there is something on the pxt side I'm missing.The text was updated successfully, but these errors were encountered: