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)



  • 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. – Karl Knechtel Jul 03 '20 at 07: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? – Karl Knechtel Jul 03 '20 at 07:04
  • Can you please show the desired output? – Pynchia Jul 03 '20 at 07:35
  • Have a look at the [Fisher-Yates](https://en.wikipedia.org/wiki/Fisher–Yates_shuffle) shuffle for ideas. – rossum Jul 03 '20 at 07:38
  • Thanks. All I needed was the shuffle function :) – Christian Magelssen Jul 10 '20 at 12:18

2 Answers2

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']
Pynchia
  • 9,316
  • 4
  • 29
  • 41
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.

Patrick Artner
  • 43,256
  • 8
  • 36
  • 57
  • while correct, this appears to be a learning exercise for algorithms rather than a question about Python as such. – Adam Smith Jul 03 '20 at 07:49