0

I am new to Python. For an experiment, I need to build a random selector function that determines the order of runs the athlete will perform. We will have four courses (A, B, C, D) and we want the athlete to perform these in random order. There will be a total of 12 runs for each athlete and each course must have 3 runs each session. How can I build this function?

This is what I have tried so far. It works but I need to run the script several times but I get what I want. So if someone has any better idea, I would be really happy.

Best Christian

import random

runs = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
diffCourses = ['A', 'B', 'C', 'D']

myRandom = []

for run in runs:
    x = random.choice(diffCourses)
    myRandom.append(x)
    if myRandom.count('A') != 3 or myRandom.count('B') != 3 or myRandom.count('C') != 3 or myRandom.count('D') != 3:
        print('The run order does not satify the requirement')
    else:
        print('satified')

print(myRandom)



5
  • If you had to do this by hand, would you write out some random letters, count whether you wrote enough of each letter, and start over until you got it right? No? Can you think of a smarter way to do it by hand? Then the next step is to write the equivalent code. If you don't know how to do that, then it at least gets you to a better question to ask. Commented Jul 3, 2020 at 7:02
  • I bet if you had been asked to include 13 of each letter instead of 3, it would be easier for you to see the intended solution. But as an additional hint: what if you also had a deck of cards at your disposal? Commented Jul 3, 2020 at 7:04
  • Can you please show the desired output? Commented Jul 3, 2020 at 7:35
  • Have a look at the Fisher-Yates shuffle for ideas. Commented Jul 3, 2020 at 7:38
  • Thanks. All I needed was the shuffle function :) Commented Jul 10, 2020 at 12:18

2 Answers 2

1

To keep things simple I would create the total set of runs first, then shuffle it

from random import shuffle 

diffCourses = ['A', 'B', 'C', 'D']
runs = diffCourses*3
shuffle(runs)
print(runs)

for example it produces

['C', 'C', 'D', 'C', 'D', 'A', 'A', 'D', 'B', 'B', 'B', 'A']
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks :) What I needed
If my answer suits you best, please mark it as such. Many thanks
0

You choose a random ordering of A,B,C,D three times and collect them into a run:

import random
diffCourses = ['A', 'B', 'C', 'D']
runs = [ a for b in (random.sample(diffCourses, k=4) for _ in range (3)) for a in b]
print(runs)

Output (additional spaces between each sample):

['A', 'D', 'C', 'B',     'A', 'B', 'D', 'C',     'B', 'A', 'D', 'C']

The random.sample(diffCourses, k=4) part shuffles ABCD in a random fashion and the nested list comprehension creates a flat list from the three sublists.

This automatically ensures you get every letter trice and in a random fashion - you might get A A if your runner needs to run A last and first ins 2 sessions.

See

for how list comps work.

1 Comment

while correct, this appears to be a learning exercise for algorithms rather than a question about Python as such.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.