Page 1 of 1

micropython password generator help

Posted: Thu Apr 16, 2020 4:58 am
by koalaburger
Hi everyone

I'm a little stuck with this code... I can't get random.shuffle to work, I'm quite certain I didn't do this quite right...

Any help would be most appreciated.

Thanks!



---------------------------------

Re: micropython password generator help

Posted: Thu Apr 16, 2020 7:44 am
by jimmo
hi,

Unfortunately the micro:bit version of MicroPython doesn't implement random.shuffle.

Here's a simple implementation you can use:

Code: Select all

def random_shuffle(seq):
    l = len(seq)
    for i in range(l):
        j = random.randrange(l)
        seq[i], seq[j] = seq[j], seq[i]
FYI, as an Australian teacher, have you heard of Grok Learning and the Australian Computing Academy. They have a whole bunch of micro:bit / MicroPython resources, and due to COVID-19 they're currently making all their resources free. (And it was all already free for teachers anyway)

https://groklearning.com/COVID19-schools/
https://aca.edu.au/ (Some of the newer micro:bit courses are really cool)

(Disclaimer, I used to work for Grok, wrote their micro:bit simulator and worked on some of their earlier micro:bit courses)

Re: micropython password generator help

Posted: Thu Apr 16, 2020 10:47 pm
by koalaburger
Thanks for this mate - much appreciated!

Yep, we've been using grok learning this year, but only in this last few weeks have we really got everyone finally on board.

I can sort of make sense of your function - but I'm trying to work out how and where to use it...
Unfortunately, the micropython courses on Grok doesn't get into this much detail.

Re: micropython password generator help

Posted: Thu Apr 16, 2020 11:21 pm
by jimmo
Basically
koalaburger wrote:
Thu Apr 16, 2020 10:47 pm
I can sort of make sense of your function - but I'm trying to work out how and where to use it...
If you want to know more, it's a Fisher Yates shuffle, https://en.wikipedia.org/wiki/Fisher%E2 ... es_shuffle

Just chuck it somewhere in the file and then replace the line "random.shuffle(tempList)" with "random_shuffle(tempList)".
koalaburger wrote:
Thu Apr 16, 2020 10:47 pm
Unfortunately, the micropython courses on Grok doesn't get into this much detail.
Yeah, last time I checked there was one or two questions involving random.choice()... turns out it's quite hard to make the auto-marker work correctly when the program is allowed to use random, while still allowing all the different ways the student can solve the problem! :)
koalaburger wrote:
Thu Apr 16, 2020 10:47 pm
I think it's beyond my year 8s (and me) to do what you did...
Yeah, unfortunately MicroPython does have a bit of a cliff I guess when you hit the limits of what's built in. In this case I happened to know that micropython-lib has some extra helpers for random -- https://github.com/micropython/micropython-lib/ -- and so I adapted the code from there.