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!
---------------------------------
micropython password generator help
-
- Posts: 2
- Joined: Thu Apr 16, 2020 4:43 am
micropython password generator help
Last edited by koalaburger on Sun Jul 11, 2021 9:38 am, edited 4 times in total.
Re: micropython password generator help
hi,
Unfortunately the micro:bit version of MicroPython doesn't implement random.shuffle.
Here's a simple implementation you can use:
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)
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]
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)
-
- Posts: 2
- Joined: Thu Apr 16, 2020 4:43 am
Re: micropython password generator help
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.
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.
Re: micropython password generator help
Basically
Just chuck it somewhere in the file and then replace the line "random.shuffle(tempList)" with "random_shuffle(tempList)".
If you want to know more, it's a Fisher Yates shuffle, https://en.wikipedia.org/wiki/Fisher%E2 ... es_shufflekoalaburger wrote: ↑Thu Apr 16, 2020 10:47 pmI can sort of make sense of your function - but I'm trying to work out how and where to use it...
Just chuck it somewhere in the file and then replace the line "random.shuffle(tempList)" with "random_shuffle(tempList)".
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 pmUnfortunately, the micropython courses on Grok doesn't get into this much detail.
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.koalaburger wrote: ↑Thu Apr 16, 2020 10:47 pmI think it's beyond my year 8s (and me) to do what you did...