micropython password generator help

Questions and discussion about running MicroPython on a micro:bit board.
Target audience: MicroPython users with a micro:bit.
Post Reply
koalaburger
Posts: 2
Joined: Thu Apr 16, 2020 4:43 am

micropython password generator help

Post by koalaburger » Thu Apr 16, 2020 4:58 am

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!



---------------------------------
Last edited by koalaburger on Sun Jul 11, 2021 9:38 am, edited 4 times in total.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: micropython password generator help

Post by jimmo » Thu Apr 16, 2020 7:44 am

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)

koalaburger
Posts: 2
Joined: Thu Apr 16, 2020 4:43 am

Re: micropython password generator help

Post by koalaburger » Thu Apr 16, 2020 10:47 pm

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.
Last edited by koalaburger on Sat Jul 04, 2020 11:30 am, edited 1 time in total.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: micropython password generator help

Post by jimmo » Thu Apr 16, 2020 11:21 pm

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.

Post Reply