random.sample or similar?
-
- Posts: 19
- Joined: Tue Jun 28, 2022 11:24 am
random.sample or similar?
Hi all.
New to MicroPython and a newbie programmer.
I'm trying to build a gallery target for my air gun shooting, using the Pi Pico to control various servos, and LCD screen with rotary encoder for some sort of menu system. I was hoping to get the targets popping up in random sequence. The targets are controlled by individual servos (10 probably), and they should only pop up once per cycle.
I looked into using "randint", but although that would give me random intergers which I can work with, it can give me repeats. Searching the Python sites, it looks like "random.sample" might work. However that is not an option for MicroPython.
Any other suggestions? Or should I switch to another language that might support what I need from it?
Thanks in advance.
Adrian
New to MicroPython and a newbie programmer.
I'm trying to build a gallery target for my air gun shooting, using the Pi Pico to control various servos, and LCD screen with rotary encoder for some sort of menu system. I was hoping to get the targets popping up in random sequence. The targets are controlled by individual servos (10 probably), and they should only pop up once per cycle.
I looked into using "randint", but although that would give me random intergers which I can work with, it can give me repeats. Searching the Python sites, it looks like "random.sample" might work. However that is not an option for MicroPython.
Any other suggestions? Or should I switch to another language that might support what I need from it?
Thanks in advance.
Adrian
- OlivierLenoir
- Posts: 126
- Joined: Fri Dec 13, 2019 7:10 pm
- Location: Picardie, FR
Re: random.sample or similar?
You can use random.choice().
Output is:
Code: Select all
from random import choice
targets = ['Target1', 'Target2', 'Target3', 'Target4']
for t in range(10):
target = choice(targets)
print(f'{t} - {target}')
Code: Select all
0 - Target4
1 - Target1
2 - Target1
3 - Target3
4 - Target3
5 - Target4
6 - Target1
7 - Target4
8 - Target2
9 - Target3
Olivier Lenoir
https://gitlab.com/olivierlenoir
https://gitlab.com/olivierlenoir
- OlivierLenoir
- Posts: 126
- Joined: Fri Dec 13, 2019 7:10 pm
- Location: Picardie, FR
Re: random.sample or similar?
As you just want to pop a target once. You do this:
You get this:
Code: Select all
from random import choice
targets = ['Target1', 'Target2', 'Target3', 'Target4', 'Target5', 'Target6', 'Target7', 'Target8', 'Target9', 'Target10']
count_target = range(len(targets))
for t in count_target:
target = choice(targets)
targets.remove(target)
print(f'{t} - {target}')
Code: Select all
0 - Target4
1 - Target8
2 - Target10
3 - Target9
4 - Target2
5 - Target5
6 - Target1
7 - Target3
8 - Target7
9 - Target6
Olivier Lenoir
https://gitlab.com/olivierlenoir
https://gitlab.com/olivierlenoir
-
- Posts: 19
- Joined: Tue Jun 28, 2022 11:24 am
Re: random.sample or similar?
Thanks for that Olivier. This was exactly how I thought the workaround would be. Removing the "choice" after it has been "chosen" after each round. You saved me a few days trying to google how to do it!OlivierLenoir wrote: ↑Sun Jul 03, 2022 6:33 amAs you just want to pop a target once. You do this:Code: Select all
from random import choice targets = ['Target1', 'Target2', 'Target3', 'Target4', 'Target5', 'Target6', 'Target7', 'Target8', 'Target9', 'Target10'] count_target = range(len(targets)) for t in count_target: target = choice(targets) targets.remove(target) print(f'{t} - {target}')
For my learning what this the "f" in this line?
Code: Select all
print(f'{t} - {target}')
- OlivierLenoir
- Posts: 126
- Joined: Fri Dec 13, 2019 7:10 pm
- Location: Picardie, FR
Re: random.sample or similar?
Code: Select all
print(f'{t} - {target}')
Code: Select all
print('{t} - {target}'.format(t=t, target=target))
Olivier Lenoir
https://gitlab.com/olivierlenoir
https://gitlab.com/olivierlenoir
Re: random.sample or similar?
Just to make it easier to search for more information, technically it's a "formatted string literal" (which other languages call "string interpolation"). "string formatting" generally refers to all the different ways of making a formatted string (including the % operator and str.format).
https://docs.python.org/3/tutorial/inpu ... -f-strings
- OlivierLenoir
- Posts: 126
- Joined: Fri Dec 13, 2019 7:10 pm
- Location: Picardie, FR
Re: random.sample or similar?
Another solution is to mimic the shuffle function.
Same solution without target variable.
Code: Select all
from random import randrange
targets = ['Target1', 'Target2', 'Target3', 'Target4', 'Target5', 'Target6', 'Target7', 'Target8', 'Target9', 'Target10']
count_target = len(targets)
for _ in range(count_target):
target = targets.pop(randrange(0, count_target - 1))
targets.append(target)
print(targets)
Code: Select all
from random import randrange
targets = ['Target1', 'Target2', 'Target3', 'Target4', 'Target5', 'Target6', 'Target7', 'Target8', 'Target9', 'Target10']
count_target = len(targets)
for _ in range(count_target):
targets.append(targets.pop(randrange(0, count_target - 1)))
print(targets)
Olivier Lenoir
https://gitlab.com/olivierlenoir
https://gitlab.com/olivierlenoir