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

Repeat Pick Random not random adafruit curcuit playground blocks #1179

Open
asaawens opened this issue Jul 24, 2020 · 3 comments
Open

Repeat Pick Random not random adafruit curcuit playground blocks #1179

asaawens opened this issue Jul 24, 2020 · 3 comments

Comments

@asaawens
Copy link

https://makecode.com/_CYU1mq4atcRV

I loaded this on 2 Adafruit Curcuit Playgound devices. They synch start, but despite being told to pick a random number of repeat times, both controllers repeat the loops the same amount of times. So the sequence progresses through the music in the same way. Why would the random seed be the same each time it is run?

Describe the bug*
A clear and concise description of what the bug is.

To Reproduce
Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
Add screenshots to help explain your problem. You can copy paste the screenshot in the github report. The .gif screen recording is very useful as well.

Desktop (please complete the following information):

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context
Add any other context about the problem here.

@riknoll
Copy link
Member

riknoll commented Aug 17, 2020

@mmoskal FYI

@mmoskal
Copy link
Member

mmoskal commented Aug 18, 2020

feel free to ping me when you're doing next release

@smagoun
Copy link

smagoun commented Jan 8, 2021

I ran into this too. I believe the random seed is hardcoded for the SAMD21 boards (like CPX), and this is an issue upstream rather than in pxt-adafruit.

For example a simple program that prints 20 random numbers in the range 0-10:

input.buttonB.onEvent(ButtonEvent.Click, function () {
    for (let i = 0; i < 20; i++) {
        console.log(Math.randomRange(0, 10))
    }
})

...will always start:
0,6,0,6,0,0,1,9,4,2,10,3,10,3,8,10,10,5,3,2

See the initRandomSeed() implementation here; the seed is fixed:
https://github.com/microsoft/pxt-common-packages/blob/master/libs/core---samd/platform.cpp#L121

The core platform has an implementation of initRandomSeed() that takes input from the temperature + light sensors, which should make it more random; maybe that could be ported to core---samd?
https://github.com/microsoft/pxt-common-packages/blob/master/libs/core/platform.cpp#L8

As a workaround I made a function that uses the light + sound sensors to add entropy:

let rand = 0
function getRandom(min: number, max: number): number {
    rand = Math.randomRange(0, 255)
    rand = rand + input.soundLevel()
    rand = rand + input.lightLevel()
    rand = rand / 3
    rand = Math.round((rand % (max - min)) + min)
    return rand
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants