random.sample or similar?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
twodoctors
Posts: 19
Joined: Tue Jun 28, 2022 11:24 am

random.sample or similar?

Post by twodoctors » Sat Jul 02, 2022 10:48 pm

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

User avatar
OlivierLenoir
Posts: 126
Joined: Fri Dec 13, 2019 7:10 pm
Location: Picardie, FR

Re: random.sample or similar?

Post by OlivierLenoir » Sun Jul 03, 2022 6:13 am

You can use random.choice().

Code: Select all

from random import choice
targets = ['Target1', 'Target2', 'Target3', 'Target4']
for t in range(10):
    target = choice(targets)
    print(f'{t} - {target}')
Output is:

Code: Select all

0 - Target4
1 - Target1
2 - Target1
3 - Target3
4 - Target3
5 - Target4
6 - Target1
7 - Target4
8 - Target2
9 - Target3

User avatar
OlivierLenoir
Posts: 126
Joined: Fri Dec 13, 2019 7:10 pm
Location: Picardie, FR

Re: random.sample or similar?

Post by OlivierLenoir » Sun Jul 03, 2022 6:33 am

As 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}')
You get this:

Code: Select all

0 - Target4
1 - Target8
2 - Target10
3 - Target9
4 - Target2
5 - Target5
6 - Target1
7 - Target3
8 - Target7
9 - Target6

twodoctors
Posts: 19
Joined: Tue Jun 28, 2022 11:24 am

Re: random.sample or similar?

Post by twodoctors » Sun Jul 03, 2022 8:28 am

OlivierLenoir wrote:
Sun Jul 03, 2022 6:33 am
As 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}')
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! :lol:

For my learning what this the "f" in this line?

Code: Select all

    print(f'{t} - {target}')

User avatar
OlivierLenoir
Posts: 126
Joined: Fri Dec 13, 2019 7:10 pm
Location: Picardie, FR

Re: random.sample or similar?

Post by OlivierLenoir » Mon Jul 04, 2022 4:29 am

Code: Select all

print(f'{t} - {target}')
It's a String Formatting. it is equivalent to this:

Code: Select all

print('{t} - {target}'.format(t=t, target=target))

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

Re: random.sample or similar?

Post by jimmo » Mon Jul 04, 2022 5:56 am

twodoctors wrote:
Sun Jul 03, 2022 8:28 am
For my learning what this the "f" in this line?
OlivierLenoir wrote:
Mon Jul 04, 2022 4:29 am
It's a String Formatting
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

User avatar
OlivierLenoir
Posts: 126
Joined: Fri Dec 13, 2019 7:10 pm
Location: Picardie, FR

Re: random.sample or similar?

Post by OlivierLenoir » Mon Jul 04, 2022 6:46 pm

Another solution is to mimic the shuffle function.

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)
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):
    targets.append(targets.pop(randrange(0, count_target - 1)))
print(targets)

Post Reply